Using MongoDb in PHP Applications
After recently attending the MongoUK conference in London, it is clear that MongoDB is fast finding application amongst cutting-edge web developers. As a relatively new concept for persistence, NoSQL (Not Only SQL) and more specifically document-oriented databases, such as MongoDB, are starting to enter the web applications landscape. Its strength lies in speed and ability to cope with dynamic data, making its goals align closely with requirements of many websites around today.
This tutorial will show you how to incorporate MongoDB into new or existing object-oriented applications, by showing how to interact and integrate with applications and how to deploy applications using MongoDB. Credit for this approach must go to Matthew Weier O'Phinney who spoke on this topic at the DPC 2010 conference in June. I would suggest reading this tutorial if you are considering using MongoDB for an application and are looking for a starting point on which to build an idea of its features. Alternatively, you may also be interested in this tutorial if you've found yourself getting tied to the persistence layer in the past and are looking for ways to reduce that technology lock-in. If you've implemented MongoDB into many of your systems, then maybe this tutorial will, open your eyes on a new way of integrating it. Whatever your background, if you understand object-orientation and want to start using MongoDB, then this tutorial is for you.
I had been wanting to research MongoDB and attempt to build something with it, however most of the tutorials I found seemed to make the assumption that all business models are arrays. Personally I prefer objects for models so I searched for a better solution, but found nothing of an OO implementation. Recalling the talk I attended at DPC, I decided to implement my application using the method suggested, mixed with some of my experience with Propel. This article is about the lessons I learned along the way.
MongoDb is a really useful technology when deployed in the correct situation. That said, it is by no means a silver bullet to persistence issues. Schema flexibility - from the dynamic-schema system it uses - is very useful for dynamic data but it is not relational. Therefore if the data for your website is highly relational, for example a record shop, then MongoDB may not be the best choice. It may be ideal for an area of the record shop, for example the custom-built analytics tracking. The best example of a data structure that is suited for MongoDB is when the fields are unknown, similar to a use case which would make entity-attribute-value (EAV) model a good choice.
One of the main benefits of using MongoDB is speed. It is also very easy to shard if the need arises, since it is designed for easy horizontal scaling. This and its mirroring servers options reduce the size of task for implementing high availability.
Document oriented storage uses the concept of storing each item as a document within your collection (database). This means that each document you retrieve from your database completely describes the entire item, rather than just a part of it represented by a row in a relational system. This also allows the system to implement a dynamic schema, which means that each item has its own schema. The advantages of this design are that each document can be slightly different, drastically different or completely the same, there are no restrictions. For example we could store objects containing all the data to display a page, allowing the specifics of each page to differ. This approach is generally more suited to discrete data sets. An example of a non-discrete data set could be a football league's data. Scenarios such as advertising campaigns where the application could be considered a discrete system with strict requirements on speed, flexibility of data and full querying would be ideal.
Using MongoDB in an Application
Another example of a useful application of MongoDB could be where caching is required but it needs to be more structured than a simple in memory cache. For this you can ignore writing to disk and make use of the best speed MongoDB has to offer. CMS systems often require very fluid data structures which often result in relational database anti-patterns being deployed and then the software has to try and compensate for these performance penalties. Instead, a document database such as MongoDB could be used to overcome this issue. The primary application of MongoDB is suggested by
Truncated by Planet PHP, read more at the original (another 50675 bytes)