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:

  first_name: 'Andrew', 
  last_name: 'Glover', 
  contacts: [ 
              type: 'cell', 
              value: '555-555-5555', 
              last_updated: 2012-09-01 23:41:51 UTC 
             { type: 'home',
               value: '555-555-5551',
               last_updated: 2012-02-11 12:21:11 UTC 

To find this document by a phone number is easy:


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:

db.persons.find({'contacts.last_updated': {$gte: datetime(2012, 8, 1)}})

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:

Reference: MongoDB From the Trenches: Masochistic Embedded Collections from our JCG partner Andrew Glover at the The Disco Blog blog.

Related Whitepaper:

Professional NoSQL

A hands-on guide to leveraging NoSQL databases!

NoSQL databases are an efficient and powerful tool for storing and manipulating vast quantities of data. Most NoSQL databases scale well as data grows. In addition, they are often malleable and flexible enough to accommodate semi-structured and sparse data sets. This comprehensive hands-on guide presents fundamental concepts and practical solutions for getting you ready to use NoSQL databases. Expert author Shashank Tiwari begins with a helpful introduction on the subject of NoSQL, explains its characteristics and typical uses, and looks at where it fits in the application stack. Unique insights help you choose which NoSQL solutions are best for solving your specific data storage needs.

Get it Now!  

Leave a Reply

five − = 0

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on 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.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! 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.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books