Kanka
Kanka

Reputation: 317

Aggregate collection that have an aggregate collectin

I am having some trouble which schema design to pick, i have a document which holds user info each user have a very big set of items that can be up to 20k items.

an item have a date and an id and 19 other fields and also an internal array which can have 20-30 items , and it can be modified,deleted and of course newly inserted and queried by any property that it holds.

so i came up with 2 possible schemas.

1.Putting everything into a single docment

    {_id:ObjectId("") type:'user' name:'xxx' items:[{.......,internalitems:[]},{.......,internalitems:[]},...]}
    {_id:ObjectId("") type:'user' name:'yyy' items:[{.......,internalitems:[]},{.......,internalitems:[]},...]}

2.Seperating the items from the user and letting eachitem have its own document

    {_id:ObjectId(""), type:'user', username:'xxx'}
    {_id:ObjectId(""), type:'user', username:'yyy'}
    {_id:ObjectId(""), type:'useritem' username:'xxx' item:{.......,internalitems:[]}]}
    {_id:ObjectId(""), type:'useritem' username:'xxx' item:{.......,internalitems:[]}]}
    {_id:ObjectId(""), type:'useritem' username:'yyy' item:{.......,internalitems:[]}]}
    {_id:ObjectId(""), type:'useritem' username:'yyy' item:{.......,internalitems:[]}]}

as i explained before a single user can have thousands of items and i have tens of users, internalitems can have 20-30 items, and it has 9 fields

considering that a single item can be queried by different users and can be modified only by the owner and another process.

on a side note i will be sharding and i have a single collection for everything.

Upvotes: 0

Views: 118

Answers (1)

Kenneth
Kenneth

Reputation: 300

I wouldn't recommend the first approach, there is a limit to the maximum document size:

"The maximum BSON document size is 16 megabytes.

The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API. See mongofiles and the documentation for your driver for more information about GridFS."

Source: http://docs.mongodb.org/manual/reference/limits/

There is also a performance implication if you exceed the current allocated document space when updating (http://docs.mongodb.org/manual/core/write-performance/ "Document Growth").

Your first solution is susceptible to both of these issues.

The second one is (Disclaimer: In the case of 20-30 internal items) is less susceptible of reaching the limit but still might require reallocation when doing updates. I haven't had this issue with a similar scenario, so this might be the way to go. And you might wanna look into Record Padding(http://docs.mongodb.org/manual/core/record-padding/) for some more details.

And, if all else fails, you can always split the internal items out as well.

Hope this helps!

Upvotes: 3

Related Questions