1

I am trying to do some de-normalization before growing my MongoDB too much. Currently I have a Users and a Rooms collection. Users can join Rooms and Rooms hold Users. Users can approach millions over time, and Rooms probably will grow to the thousands. Rooms can have thousands of Users. Should I store the full User documents in each Room document or just the ObjectID? Or should I store the Room document's ObjectID in an array under each User document? I know Joins are frowned upon, but these will grow at an incredible rate. I also need to do complex calculations over all the Users in a Room. The relationship is Many-to-Many. Currently I am going to store an array for each Room with the ObjectIDs of each User. I am also going to store an array for each User with the ObjectIDs of each Room they are in. Does this make sense?

Should I do Object embedding or should I do Object references for my situation?

Even Steven
  • 137
  • 6
  • I'm referring to this question's top answer: https://stackoverflow.com/questions/24096546/mongoose-populate-vs-object-nesting Is this code dependable? Doesn't it suffer from the possibility that order.items might run before order is complete running its query? Shouldn't a callback be used? If no, why not? var order = db.orders.findOne({ "_id": ObjectId("5392fea00ff066b7d533a765") }); order.items = db.items.find({ "_id": { "$in": order.items } ).toArray(); – Even Steven Nov 16 '17 at 21:47
  • 1
    To be clear here. "Should I do...?" is not really a question for anyone here to answer, which is essentially what the answers you have been linked to say. How your application stores the data is a factor of how you application uses the data. Your "overall usage pattern" is therefore too broad a question to ask. On the specifics of your "referring to question", this is actually what `.populate()` of mongoose does, by "hiding that query" and handling the async calls. Of course modern MongoDB has [`$lookup`](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/) – Neil Lunn Nov 16 '17 at 22:06
  • 1
    The other main concept discussed in those answers is "any join" involves a cost, which is why MongoDB has the concept of "embedding". So it's not about someone "telling you which is best", as that's a conclusion you need to come to on your own. Same question, with the same basic solution. Go and work it out and see what works best for you. – Neil Lunn Nov 16 '17 at 22:08
  • @NeilLunn In your example, is it safe to assume that findOne will return fast enough for db.items.find to work properly? – Even Steven Nov 16 '17 at 22:37
  • You need to be reading what the existing answers already say. One of them happens to be mine. Both detail the "pros and cons" and that is really what you need to consider. Again, nobody's opinion matters here. – Neil Lunn Nov 16 '17 at 23:17
  • Dude, it's a "demonstration". Run mongoose yourself with the debugging turned on. What you will see is an `$in` query being issued exactly like I am saying. Consider that some people might possibly have "more experience" than you do and therefore a better grasp of async code and execution. Your question has been answered. Go and learn. – Neil Lunn Nov 17 '17 at 00:29

0 Answers0