Database Reference
In-Depth Information
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
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.
When people hear that BSON is a binary form of JSON, they expect it to take up much less
room than text-based JSON. However, that isn't necessarily the case; indeed, there are
many cases where the BSON version takes up more space than its JSON equivalent.
You might wonder why you should use BSON at all. After all, CouchDB (another
powerful document-oriented database) uses pure JSON, and it's reasonable to wonder
whether it's worth the trouble of converting documents back and forth between BSON
and JSON.
First, we must remember that MongoDB is designed to be fast, rather than space-
efficient. This doesn't mean that MongoDB wastes space (it doesn't); however, a small
bit of overhead in storing a document is perfectly acceptable if that makes it faster to
process the data (which it does). In short, BSON is much easier to
traverse
(that is, to look
through) and index very quickly. Although BSON requires slightly more disk space than
JSON, this extra space is unlikely to be a problem, because disks are cheap, and MongoDB
can scale across machines. The tradeoff in this case is quite reasonable: you exchange a
bit of extra disk space for better query and indexing performance.