So when you read that MongoDB is a document store, you might get the wonderful idea to store your relationships in a big document. Since mongo lets you reach into objects, you can query against them, right?
Several times, we’ve excitedly begun a schema this way, only to be forced to pull the nested documents out into their own collection. I’ll show you why, and why it’s not a big deal.
I’m no MongoDB expert, but the suggested solution requires: 1) two network roundtrips; 2) an additional index.
The way I’d look at this problem is:
- what is the most frequent operation: reading a blog post and all its comments or displaying all the comments of a specific user?
- assuming the answer is reading a blog post and all its comments, I’d ask myself how frequent is the other operation.
- if it’s something that I need to perform just once in a while, I’d consider using MongoDB MapReduce, even if that would be suboptimal.
- if it’s a frequent operation, then I’d consider adding a separate collection for comments. Even better, I’d add a per user collection for comments.
Original title and link: MongoDB Tips and Tricks: You Only Wish MongoDB Wasn’t Relational ( ©myNoSQL)