Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Tuesday, May 1, 2012

Alternative naming for associations in MongoMapper

MongoMapper is a mature Object Relational Mapper written in Ruby and for use with MongoDB. It roughly follows the conventions used in Rails' ActiveRecord / ActiveModel. As my use of MongoDB has grown more sophisticated, I have had some trouble discovering how to get what I need done with MongoMapper. Fortunately, this is often not a deficiency of MongoMapper, it's more that I wasn't aware of all its features.

In general on my blog, I'll point out a few features that I felt were incompletely explained in the documentation. Here I address how to use alternative naming for associations.

Using non-default naming for associations

By default, associations are named after the class name:

class Plum
   include MongoMapper::Document
   one :pit
end
class Pit
  include MongoMapper::EmbeddedDocument
end


Running the following to create a Plum with a Pit:


p = Plum.new
p.build_pit
p.save


will result in a document in mongodb like this:

{
   "_id" : ObjectId("4f9ff50ba0b7f9b71500004a"),
   "pit" : {
      "_id" : ObjectId("4f9ff51ba0b7f9b71500004b")
   }
}

However, what if you want to have the class called Pit, but have the association name called dapit? Well, you can use the :class option when declaring the association:


class Pit; end

class Plum
  include MongoMapper::Document

  one :dapit, :class => Pit 
end

class Pit
  include MongoMapper::EmbeddedDocument
  embedded_in :plum
end


Notice that in the above, I needed to declare the class of the embedded document ahead of time, so that I could refer to it in the class definition of Plum.
With the above associations, you can do the following:


p = Plum.new
p.build_dapit
p.dapit.class.name
# => "Pit"


You can do the same with a non-embedded association:


class Pit; end

class Plum
  include MongoMapper::Document

  one :dapit, :class => Pit
end

class Pit
  include MongoMapper::Document
  belongs_to :plum
end


With this version you can do the following:


p = Plum.new
p.create_dapit
p.save
p.dapit.plum

However, what if you want to rename the belongs_to association back to plum? This is a bit more complicated. You need to also specify the foreign_key so that both directions of the association use the same key name for the ObjectID:


class Pit; end

class Plum
  include MongoMapper::Document

  one :dapit, :class => Pit, :foreign_key => :daplum_id
end

class Pit
  include MongoMapper::Document
  belongs_to :daplum, :class => Plum
end


You might wonder, why do I specify the foreign key daplum_id when I'm declaring the association to dapit. Well, for one associations, the ObjectID of the referring Plum is stored in the pit collection. Then when the pit is requested, MongoMapper find the pit document that points back to the plum. So the key name that needs to be changed to match da new naming convention is plum_id -> daplum_id and it's stored in the pit collection, but this renaming is declared in the Plum class, not the Pit class.

You can use the above version as follows:


p = Plum.new
p.create_dapit
p.save
p.dapit.daplum

And the document for a pit now looks like this, using the alternatively named key:

{
   "_id" : ObjectId("4fa0002aa0b7f9653800002b"),
   "daplum_id" : ObjectId("4fa00026a0b7f9653800002a")
}

Hope that all makes sense. 

Saturday, April 28, 2012

Are many-to-many associations in MongoDB fast enough?



MongoDB has a lot of potential. It's schema-less, dynamically typed, distributed, replicated, fast, has a rich query language, broadly supported, and the list goes on.

One thing I found hard to get my mind around is when to denormalize and when to keep things normalized. The major reason to keep documents denormalized is because there is no JOIN operation, as there is in SQL. This means that it's often faster to store the associated data in the parent document, rather than join it in on demand.

However, there are circumstances in which it's not desirable to denormalize things. For example, if the associated data is not unique to single document or collection, then denormalization will require replicating data, and it would be difficult to keep all copies in sync.

I recently ran into such a situation and decided to do some benchmarking to see how slow such queries were. For the purposes of discussion, I have devised the following example layout that is identical to my original problem, but easier to explain. 

Suppose you have sets of items, and each item in each set has ratings by various people. Without JOINs, I was worried about whether it would be slow to query the ratings for all the items of a set. Here I compare two means of querying such an arrangement.

I use the same data in each case, and the end result is the same. There is a collection of items. There is a collection of sets of items. Each set is a document that contains an array of ObjectIDs for items. Then there is a collection of ratings. Each rating is a document listing the person who gave the rating, the item the rating is for, and the rating itself. The question is how fast are various ways to query all the ratings for items in a particular set. In one case, individually.js, I query the ratings collection once for each item in each set. In the other case, I issue one query per set, providing an array, and using the $in operator to get all the ratings for the set of items. While the latter is certainly fewer queries, either way needs to find all the items. Will the one with fewer (but more complicated) queries be faster?

I have put my code into this Gist.

To run the benchmark, run the setup.js script using the Mongo console:

mongo < setup.js

This will use/create a database, benchmarking.

The critical difference between the two test cases lies in how the ratings for a set are queried. This nested pair of for loops queries them one by one.


// Find user 1's ratings for all items in all sets
cur.forEach(function(set) {
  set.items.forEach(function(item_id) {
    var rating = db.ratings.findOne({user: 1, item_id: item_id});
    sum += rating.rating;
  });
});


However, there is an alternative, which is to have a single query that provides all the item_ids that are needed for a given set, as an array, using the $in operator to retrieve those ratings:


// Find user 1's ratings for all items in all sets
cur.forEach(function(set) {
  var res = db.ratings.find({user: 1, item_id: {$in: set.items}});
  res.forEach(function(rating) { sum += rating.rating; });
});


The two benchmark test cases can be run as follows, using the time function to keep track of elapsed runtime:

time mongo < individually.js

And:

time mongo < using_in.js

Benchmarking results:

Individually fetching each rating: 1.76s
Using the $in operator: 0.31s

This is on my rather old macpro. 

Thus, for associations like this, $in is clearly the better choice, performing nearly 6x faster. Also in absolute terms, the query is not slow. Each set gets the 20 associated ratings in less than 1ms.