About Andrew Glover

MongoDB From the Trenches: Masochistic Embedded Collections

MongoDB supports rich documents that can include, among other things, embedded documents. This feature embodies a has-a relationship quite nicely and can, if modeled properly, reduce the number of finds required to ascertain certain data as there are no joins in Mongo.

As classic example of embedding a collection of documents inside a parent document is contact addresses (i.e. mailing, email, twitter, etc) associated with a person. Think business cards. You can, of course, model this in any number of ways – in the traditional relational world, this would be a one-to-many relationship between at least two tables. Nevertheless, with a document-oriented database, you can model a parent person document with an embedded collection of contacts that are each themselves documents containing, say, type (i.e. phone, twitter, email) and value (which could be 555-555-555, @jon_doe, etc).

This relationship with Mongo works nicely if the child embedded document never needs to exist outside of its parent. In the case of a business card, the contact document representing a phone number, for example, doesn’t necessarily make sense outside the context of the person who it belongs to. With this relationship, you can easily find a particular person via his/her phone number (that is, via Mongo’s query language, you can reach inside arrays via its dot notion) effortlessly). And, once you have a handle to a person, you don’t need to execute a series of finds to ascertain contact information – it’s all right there.

Nevertheless, things start to get painful quickly if you’d like to operate solely on a singular embedded document. That is, if you execute finds that are intended to deal with the expected resultant embedded document, you’re in for some work: as of Mongo 2.2, you can’t select a singular document from within a collection residing in a parent via a query. A find in this case will pull everything – it’s up to you (i.e your application) to filter things.

An example will probably help: imagine the business card example from earlier – a person document containing an embedded collection of contacts:

But what if you wanted to find the contact that was recently updated, say since the beginning of the month, and change its value or add some additional meta-data? The query you’d like would look something like:

This query works and will match the person ‘Andrew Glover’ – but the catch here is that what is returned is the entire document. You can add query limiters if you’d like (i.e. {contacts:1}), however, that will merely return a person document with only a collection of contacts. Thus, you are left to iterate over the resultant collection of contacts and work your magic that way. That is, you still have to find the contact document that was edited this month! In your code!

No big deal, you say? This particular example is, indeed, a bit contrived; however, imagine if the overall document is quite large (maybe it’s not a person but an organization!) and that the embedded collection is also lengthly (how many employees does Google have?). Now this simple update is pulling a lot of bytes across the wire (and taxing Mongo in the process) and then your app is working with a lot of bytes in memory (now the document is taxing your app!). Did you want this operation to happen quickly, under load too?

Thus, with embedded document collections, if you envision having to work with a particular embedded document in isolation, it is better, at this point, to model has-a relationships with distinct collections (i.e. in this example, life would be much easier if there is a person collection and a contacts one). Indeed, the flexibility of document-oriented, schema-less data stores is a boon to rapid evolutionary development. But you still have to do some thinking up front. Unless, of course, you’re a masochist.

I’m a huge fan of Mongo. Check out some of the articles, videos, and podcasts that I’ve done, which focus on Mongo, including:

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.