3

I had like to know if somebody knows if it is possible to query the _id field with a query like this :

{$lte : ObjectId(....) , $gte : ObjectId(...)

, instead of

{$in : [ObjectId( )...lots of ObjectId.....]}

and if there is a gain in performance (i think so..)

Thanks

1 Answer 1

6
{$lte : ObjectId(....) , $gte : ObjectId(...)

Yes there is such query {_id : {$lte : ObjectId(....) , $gte : ObjectId(...)} }

The performance depends on what is the range that you have selected in 1st query. Smaller range means fast response. For 2nd query, response depends on count and range of ObjectIds in the query. The more fragmentation in the value of ObjectIds may incur large no of page faults.

Sign up to request clarification or add additional context in comments.

1 Comment

I edited Abhishek's excellent answer, putting $gte to the left and $lte to the right, as it is an inclusive range. I'm also making a mention here as a comment since this is a 5 year old solution, and my suggestion may not make it past moderation backlogs :D Thanks for your very helpful reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.