Database Reference
In-Depth Information
Keys and values always come in pairs. Unlike an RDBMS, where every field must have a value, even if it's NULL
(somewhat paradoxically, this means unknown ), MongoDB doesn't require that a document have a particular value.
For example, if you don't know the phone number for a particular person on your list, you simply leave it out.
A popular analogy for this sort of thing is a business card. If you have a fax number, you usually put it on your business
card; however, if you don't have one, you don't write: “Fax number: none.” Instead, you simply leave the information
out. If the key/value pair isn't included in a MongoDB document, it is assumed not to exist.
Implementing Collections
Collections are somewhat analogous to tables, but they are far less rigid. A collection is a lot like a box with a label on
it. You might have a box at home labeled “DVDs” into which you put, well, your DVDs. This makes sense, but there is
nothing stopping you from putting CDs or even tapes into this box if you wanted to. In an RDBMS, tables are strictly
defined, and you can only put designated items into the table. In MongoDB, a collection is simply that: a collection of
similar items. The items don't have to be similar (MongoDB is inherently flexible); however, once we start looking at
indexing and more advanced queries, you'll soon see the benefits of placing similar items in a collection.
While you could mix various items together in a collection, there's little need to do so. Had the collection been
called media , then all of the DVDs, CDs, and tapes would be at home there. After all, these items all have things in
common, such as an artist name, a release date, and content. In other words, it really does depend on your application
whether certain documents should be stored in the same collection. Performance-wise, having multiple collections
is no slower than having only one collection. Remember: MongoDB is about making your life easier, so you should do
whatever feels right to you.
Last but not least, collections are effectively created on demand. Specifically, a collection is created when you
first attempt to save a document that references it. This means that you could create collections on demand (not that
you necessarily should). Because MongoDB also lets you create indexes and perform other database-level commands
dynamically, you can leverage this behavior to build some very dynamic applications.
Understanding Databases
Perhaps the easiest way to think of a database in MongoDB is as a collection of collections. Like collections, databases
can be created on demand. This means that it's easy to create a database for each customer—your application code
can even do it for you. You can do this with databases other than MongoDB, as well; however, creating databases in
this manner with MongoDB is a very natural process. That said, just because you can create a database in this manner
doesn't mean you have to or even that you should. All the same, you have that power if you want to exercise it.
Reviewing the Feature List
Now that you understand what MongoDB is and what it offers, it's time to run through its feature list. You can find a
complete list of MongoDB's features on the database's website at www.mongodb.org/ ; be sure to visit this site for an
up-to-date list of them. The feature list in this chapter covers a fair bit of material that goes on behind the scenes, but
you don't need to be familiar with every feature listed to use MongoDB itself. In other words, if you feel your eyes
beginning to close as you review this list, feel free to jump to the end of the section!
Using Document-Oriented Storage (BSON)
We've already discussed MongoDB's document-oriented design. We've also briefly touched on BSON. As you learned,
JSON makes it much easier to store and retrieve documents in their real form, effectively removing the need for any
sort of mapper or special conversion code. The fact that this feature also makes it much easier for MongoDB to scale
up is icing on the cake.
 
Search WWH ::




Custom Search