最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - what is the maximum number of elements an array used in $nin should have? - Stack Overflow

programmeradmin4浏览0评论

The user collection in my MongoDB database needs to be searched to find content for my users. Each user has their list of blocked users.

const suggested_content = await db.collection('users').find({
  _id: { $nin: [...blockedUsers] }
}).toArray();

So the question is, in what ballpark should the maximum number of elements of the blockedUsers array be in? In other words, at about what number of elements will performing a $lookup to find and remove the block users be faster instead?

The user collection in my MongoDB database needs to be searched to find content for my users. Each user has their list of blocked users.

const suggested_content = await db.collection('users').find({
  _id: { $nin: [...blockedUsers] }
}).toArray();

So the question is, in what ballpark should the maximum number of elements of the blockedUsers array be in? In other words, at about what number of elements will performing a $lookup to find and remove the block users be faster instead?

Share Improve this question edited Mar 25 at 6:07 aneroid 16.3k3 gold badges40 silver badges75 bronze badges asked Mar 24 at 23:29 Bear Bile Farming is TortureBear Bile Farming is Torture 5,1798 gold badges41 silver badges118 bronze badges 3
  • 2 Do you really expect your users will block hundreds or even thousands of users? – Wernfried Domscheit Commented Mar 25 at 6:02
  • @WernfriedDomscheit The question is simplified. In addition to blocked users, there will also be viewed users. Viewed users also need to be removed so as not to show duplicate content. – Bear Bile Farming is Torture Commented Mar 25 at 6:07
  • 2 Welcome back, I was starting to miss your questions which are always a bit special. As a general suggestion I would propose to read this article: mongodb/blog/post/building-with-patterns-a-summary It will answer many of your concerns and you get out many ideas from it. – Wernfried Domscheit Commented Mar 25 at 6:07
Add a comment  | 

1 Answer 1

Reset to default 3
  1. An "in-array" check will always* be faster than a $lookup. Since it's part of the query.

    • Unless you're trying to compare arrays with 1 million elems vs just 1 lookup matching a single elem. Where that crossover occurs, you'll need to performance test yourself.
  2. Your query also needs to exclude the current user id. Assuming they can't block themselves, you probably don't want to recommend their own content to them. find({ _id: { $nin: [currentUserId, ...blockedUsers] } }. If the user is not signed in, there won't be any blockedUsers to check.

  3. The limitation here is more likely to be the size of the array growing so large that the document itself is larger than the 16 MB limit.

  4. The recommended size for arrays containing documents is 200-1000 elements - that's specific to querying/filtering/indexing performance with the elements in the array. Arrays which can keep growing should be split out into their own collection, with each each document having N elements. And then you'll need to use a lookup. This is roughly the Subset Pattern.

    • The recommended max elems-per-array recommendation is not clearly documented. However, it does come up in MongoDB's Data modeling vids, like this one which says 1000.
    • (Will look for the vid which says 200)
    • Schema Design Anti-Patterns: Avoid Unbounded Arrays
发布评论

评论列表(0)

  1. 暂无评论