1

Think of a standard city builder game. You have cities, each cities have units, each units have gear(equipped) and the city itself has more gear(unequipped, they are in a box somewhere, heh).

A) First off, I thought of making 3 collections.

Cities: {_id, name, locationX, locationY}
Units: {_id, name, city_id, unitType}
Items: {_id, name, itemType, unitEquipped} where unitEquipped was optional( item belonged to the city).

B) Then I turned it into:

Cities: {_id, name, locationX, locationY}
Units: {_id, units:[ {name, city_id, unitTye}, {name, city_id, unitTye}... ]}
Items: {_id, items: [ {name, itemType, unitEquipped}, {name, itemType, unitEquipped}... ]}

C) And finally, this is one collection:

Cities:
{
_id, name, locationX, locationY,
units: [ {name, unitTye}, {name, unitTye}... ],
items: [ {name, itemType, unitEquipped}, {name, itemType, unitEquipped}... ]
}

Notice how units no longer have a city_id in them as there is no need to. Can anyone tell me the pros and cons of these 3 styles? The last one i think has the least redundant data but if I want locationX, I have to deal with the other info in that document, right?

Discipol
  • 3,137
  • 4
  • 22
  • 41
  • Without extra details, an answer would be very speculative. How is the data being used? Is it being updated frequently? Are there updates from multiple users (to the same document)? How large are the documents? How frequent are the changes? How many simultaneous: users, reads, writes? – WiredPrairie May 21 '13 at 10:56
  • 3
    Also, this pattern has been discussed a lot in many forums and by 10gen. http://docs.mongodb.org/manual/core/data-modeling/ and here: http://stackoverflow.com/questions/5373198/a-simple-mongodb-question-embed-or-reference – WiredPrairie May 21 '13 at 11:03
  • Cities are queried at a medium frequency. Units and Items are highly queried. I will look over the second link, WiredPrairie. You have helped me before, I appreciate it. +1 – Discipol May 21 '13 at 11:33
  • wait, B is not a real data model. It's just A where you turned every document into an array - that is not a schema. You should consider as a third version removing city collection and renormalizing information from it into units (replace city_id with three fields from City collection - name, location coords). – Asya Kamsky May 21 '13 at 12:53
  • I usually get locationX and locationY together but sometimes apart. I agree B isn't good. I AM worried about bloating the database with entries in both nr of documents, and filesize. – Discipol May 21 '13 at 13:10

0 Answers0