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

When using MongoDB’s $in clause with Aggregate, Does That have any max limit in the 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?
If there is any limit can I used an heshing technic in order to pass huge queries?

There is 16MB limit for BSON documents, but i don’t know if it applies to queries. That said, its going to be slow. If i understand it right, you are sending your ids array over network each time you run a query - which will take many ms or even seconds, dependng on how fast the connection is.
Consider saving ids in your database and use $elemMatch or any other array query or $in (aggregation expression). Also you can add an index for your ids.

1 Like

Hi and thank you for your reply, I tested it with 5MB doc of query and it takes several seconds (23).
How can I understand what was the bottleneck ?
I’m using Atlas service and there is nothing in the profiler.
Are there is any way to profile the networking and the query time in mongo?
BTW are there any option to “stream” the query since I need only the first “10” results?
Thank you