简体   繁体   English

我如何处理MongoDB中的大数组?

[英]How do I handle big arrays in MongoDB?

I have a user document, and it has an array field called activityLog. 我有一个用户文档,它有一个名为activityLog的数组字段。 Every action the user takes needs to be recorded here. 用户采取的每个操作都需要在此处记录。 However, I know MongoDB caps document sizes at 16MB. 但是,我知道MongoDB的文档大小为16MB。 If this array needs to be able to hold potentially millions of items, won't that exceed the document size limit? 如果此阵列需要能够容纳数百万个项目,那么这不会超过文档大小限制吗? How can I handle that? 我该怎么处理?

Please answer this generally, ie some generic array needs to hold potentially millions of items 请回答这个问题,即一些通用数组需要保留数百万个项目

From the use case you have described, it looks like activity logs belong to a separate collection. 从您描述的用例,看起来活动日志属于单独的集合。 As you already mentioned you can not embed logs to a user because the data grows a large amount and can exceed the 16 MB document size, so using references is the way to go. 正如您已经提到的,您无法将日志嵌入到用户中,因为数据量增长很多,并且可能超过16 MB的文档大小,因此使用引用是可行的方法。 Next, you have to decide whether to reference from a user to logs or from logs to a user (parent-referencing). 接下来,您必须决定是从用户引用日志还是从日志引用到用户(父引用)。 Having an array of references to logs in a user is not as good as parent-referencing, because it still can overflow the document size limit, also you will hardly need all user history each time you retrieve a user. 在用户中具有对日志的引用数组不如父引用好,因为它仍然可以超出文档大小限制,每次检索用户时也几乎不需要所有用户历史记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM