Software Development

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:

db.persons.find({'contacts.value':'555-555-5555'})

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.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button