简体   繁体   中英

How do I handle big arrays in MongoDB?

I have a user document, and it has an array field called activityLog. Every action the user takes needs to be recorded here. However, I know MongoDB caps document sizes at 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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