0

After few months working with Mongo trying to understand if using sub-documents for nested data is good or not, especially in this example:

Assume users collection that each document into it have the following:

{
    "_id" : ObjectId("some valid Object ID"),
    "userName" : "xxxxx",
    "email" : "xxxx@xxxx.xx"
}

Now, in my system there are also rooms (another collection) and i want to save for each user scores per room.

In my mind, to do that i have 2 major options, (1) create new collection call userScores that will hold: userId, roomId, scores fields like i did previously in MySql and other relational DB's (2) create a sub-document into the above user document:

{
     "_id" : ObjectId("sdfdfdfdfdf"),
     "userName" : "xxxxx",
     "email" : "xxxx@xxxx.xx",
     "scores": {
          "roomIdX": 50,
          "roomIdY": 50,
          "roomIdZ": 50
     }          
}

What do you think is better way so later i can handle searches, aggregations and other data queries via the code (mongoose in my case)

Thanks.

Shlomi
  • 3,622
  • 5
  • 23
  • 34
  • Your question is primarily opinion-based. That been said; you should know that mongodb does'nt support any JOIN operation – styvane Oct 11 '15 at 07:06
  • Yes i know, but ODM's support populate operations (i know they're doing 2 queries beyond the sense but it helps and make your code stable) and looks like populate working on 2 collections FK.. – Shlomi Oct 11 '15 at 07:12
  • Just have a look at this http://stackoverflow.com/questions/32810204/mongodb-schema-performance-optimization/32811310?noredirect=1#comment53484460_32811310. – Rohit Jain Oct 11 '15 at 10:41
  • @user3100115 There is nothing opinion based about subdocuments: one-to-few relations work, one-to-many relations are limited by the BSON size limit(and cause frequent expensive document migrations in the mmapv1 storage engine). – Markus W Mahlberg Oct 11 '15 at 11:14
  • You can find some exhaustive answers about this here http://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference – Oren Yakobi Oct 11 '15 at 13:29

0 Answers0