Maximum Array size to use in $in

1,351 views
Skip to first unread message

Soorya Prakash

unread,
Nov 5, 2015, 4:36:24 AM11/5/15
to mongodb-user, boopathi s, Baskaran R
Hi , 
Can anyone please  tell me , what can be the maximum array size  for the  $in in a find query or in an aggregate query.If the array size goes beyond 5000 , will it cause an performance or out of memory issues. I need to use $in with $limit query from the 4000 "_id"s.

Stephen Steneker

unread,
Nov 6, 2015, 8:42:02 AM11/6/15
to mongodb-user

On Thursday, 5 November 2015 20:36:24 UTC+11, Soorya Prakash wrote:

Can anyone please tell me , what can be the maximum array size for the $in in a find query or in an aggregate query.If the array size goes beyond 5000 , will it cause an performance or out of memory issues. I need to use $in with $limit query from the 4000 “_id”s.

Hi Soorya,

The maximum size for a find or aggregation query is the same as the BSON document size, which is currently 16MB.

You can estimate the size of a query document using Object.bsonsize() in the mongo shell.

For example, creating arrays of ObjectIDs:

    var n = 4000;
    var query = [];

    while (query.length < n) { query.push(new ObjectId()) }

    > Object.bsonsize(query);
    70895

The performance impact will depend on your actual query criteria and options (eg. sort/limit), version of MongoDB, and environment/deployment.

I recommend you test in a dev/staging environment and review the explain output to understand how queries scale for your use case.

If you have questions about optimizing a specific query, please followup with more details (eg. query, MongoDB server version, explain output, ...).

Regards,
Stephen

Reply all
Reply to author
Forward
0 new messages