2

I have a project that I'm working on that will require me to store a large number of objects in an array linked to a parent object, akin to the storing of social media comments to their original post. What is the best way for me to organize the data for the array of child documents/comments?

Is it considered best practice to have the child objects under a different collection and reference to their parent or would it be more ideal just to put them all within the parent object directly?

Community
  • 1
  • 1
stanier
  • 45
  • 3

1 Answers1

2

I discuss this a little here, read this first: https://stackoverflow.com/a/27285313/68567

For your case, Option 3 (keeping some of the data in your primary model) is probably the best. The key is to Avoid unbounded array growth.

This has to do with how Mongodb allocates documents. http://docs.mongodb.org/manual/core/storage/ "Every document in MongoDB is stored in a record which contains the document itself and extra space, or padding, which allows the document to grow as the result of updates."

When node allocates new documents it allocates space based on the size of the inserted document and the sizes of documents already in your collection. (Read more in the link above.) If you have some documents that are orders of magnitude larger than others this will likely lead to fragmentation.

The way to avoid having too many documents in your 'comments' sub-document array is with the $push and $slice commands. http://docs.mongodb.org/manual/reference/operator/update/slice/

So store the 'most recent 5' and display those when the item first loads. (Or oldest, or whatever other sorting criteria you want to use.) Then provide a way for the user to load more which will do a separate round-trip to the collection that has all of them.

Community
  • 1
  • 1
Will Shaver
  • 12,471
  • 5
  • 49
  • 64