在Mongo中组织子文档的最佳方式? [英] Best way to organize subdocuments in Mongo?

查看:130
本文介绍了在Mongo中组织子文档的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我正在工作,将需要我存储大量的对象在链接到父对象的数组,类似于社交媒体评论存储到他们的原始帖子。对于组织子文档/注释数组的数据,最好的方法是什么?



它被认为是将子对象放在不同集合下的最佳实践,引用它们的父对象,还是更理想的只是将它们全部放在父对象中?

解决方案

小这里,请先阅读:
http://stackoverflow.com/a/27285313/68567 p>

对于你的情况,选项3(在你的主模型中保留一些数据)可能是最好的。关键是避免无界阵列增长



这与Mongodb如何分配文档有关。 http://docs.mongodb.org/manual/core/storage/
MongoDB中的每个文档都存储在一个记录中,该记录包含文档本身和额外的空间,或者填充,允许文档作为更新的结果增长。



当节点分配新文档时,它会根据插入文档的大小和集合中已有文档的大小来分配空间。 (阅读更多在上面的链接。)如果你有一些文档的数量级大于其他这将可能导致碎片。



避免太多的方法你的'comments'子文档数组中的许多文档都使用$ push和$ slice命令。
http://docs.mongodb.org/manual/reference/操作符/更新/切片/



因此,存储最近5并在项目首次加载时显示。 (或最旧的,或任何其他排序标准你想使用。)然后提供一个方法,用户加载更多,将做一个单独的往返到所有的集合。


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?

解决方案

I discuss this a little here, read this first: http://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.

这篇关于在Mongo中组织子文档的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆