Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MongoDB's $in clause has any max limit in number of arguments

When using MongoDB's $in clause with Aggregate , Does That has any max limit in number of arguments ?

for example

Model.aggregate(
        [
            { $match : 
                { '_Id' : 
                            {
                                $in : ids
                            }
                }
            } ,
            { $group : 
                {   _id : '$roomId' ,
                    maxdate: { $max: "$date"},
                }
            },
            {$sort: { maxdate: -1} },
            {$skip: skip},
            {$limit: limitNum }
        ]

In ids array , how many ids i can pass ?

Currently i am not facing any issue with ids length till 50,000 ... but for safe side wanted to know the max limit.

I have tried to search on Mongo doc , but didnt find anything.

Thanks in advance.

like image 563
kamlesh patel Avatar asked Feb 24 '16 11:02

kamlesh patel


People also ask

What is the limit of $in in MongoDB query?

MongoDB limits document sizes (as of version 2.4. 0+) to 16 MB. So, no matter what the size of an individual number, it's the same bsonsize.

Does MongoDB have a limit?

The maximum size an individual document can be in MongoDB is 16MB with a nested depth of 100 levels. Edit: There is no max size for an individual MongoDB database.

What is the limitation of a document in MongoDB?

The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.

What does $IN do in MongoDB?

The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype: { field: { $in: [<value1>, <value2>, ... < valueN> ] } } For comparison of different BSON type values, see the specified BSON comparison order.


1 Answers

There is no limit to the number of arguments in the $in clause itself, however, the total query size is limited to 16MB as a query is just a BSON document. Depending on the type used for ids (see the BSON specification), you may start running into problems when your ids length is in the order of a few millions.

like image 125
Alex Avatar answered Oct 16 '22 10:10

Alex