0
votes

I need to find all the documents having some fixed _id in a mongo collection in java. Example : find all the documents where _id = 1 or _id = 100 or _id = 200 and so on. The no of such queries could be around 50000. Is there any option to combine all these unique queries into one query?

I know we could do that using $or operator in mongo db java driver , would that be slow?

There is also a bulk find operation in java , but that works on DBCollection object not on MongoCollection object , and it is also depricated now so don't want to use it.

1

1 Answers

0
votes

You can combine all ids into array and use $in operator

{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }

example:

db.inventory.find( { qty: { $in: [ 5, 15 ] } } )

I hope this helps