2

I think its fair to say I'm pretty new to MongoDB having just installed the database this afternoon.

I'm managing to get to grips with storing and retrieving objects but am struggling to find the best way of storing objects that have a many-to-many relationship.

I've already come accross the DBRef object and have got that working but this seems to only support a lazy loaded approach.

Is there a way to encourage MongoDB to eagerly load DBRefs?
Is there a better/different way to store the many-to-many relationship?

Many thanks

Rob

Rob Forrest
  • 7,329
  • 7
  • 52
  • 69
  • Can you be more specific about the type of relationship you'd like to store? Mongo's document structures is flexible, and not all many-to-many relationships should necessarily be represented the same way. There are a lot of good examples of different schemas here: http://www.mongodb.org/display/DOCS/Schema+Design – matulef May 17 '12 at 17:09
  • Gates VP posted a link which has a perfect example of "user - campaign - userCampaign". In this its been suggested that a list of ids gets stored, is storing an ID better than storing a DBRef? – Rob Forrest May 18 '12 at 09:22

1 Answers1

3

So first, I think you need to look at this question over here, which talks about many-to-many relationships.

The other thing to understand is the nature of "DBRefs". The MongoDB database does not provide any join functionality.

The DBRef is just a standard that several library / driver implementers agreed upon a couple of years ago. The DBRef is just a JSON object in a specific notation that provides a pointer to some other document in some collection. So the implementation of "lazy vs. eager" loading is completely specific to the driver / wrapper library that you are using.

That stated, the concept of "eager loading" is rather pointless with MongoDB. In SQL you can potentially save on total queries by using some form of eager loading and doing a join "in advance". Again, the DB does not support joins, so "eager loading" takes the same number of queries as "lazy loading".

Community
  • 1
  • 1
Gates VP
  • 44,957
  • 11
  • 105
  • 108
  • 1
    I also wanted to point out that even the mongo docs say it can be more efficient to simply store the ids manually if you know the collection will stay the same. Personally I dont use DBref and just store ID's and resolve them when needed. – jdi May 17 '12 at 18:42
  • 1
    That raises an interesting point then. Does this then mean that if the MongoDB is hosted on a different machine to the web server then there will be a round trip for each document request? I note that doing a find() returns a cursor with each document being retrieved on iteration. – Rob Forrest May 18 '12 at 09:13
  • @RobForrest this depends on your implementation. If you have two collections: A & B and you load everything from A *then* everything from B you can probably do this in two queries. However, if you do simple "lazy" loading inside of a `for` loop, you could definitely incur a lot of overhead. So you probably want to implement both a "join" and "batch join" feature. – Gates VP May 18 '12 at 18:11