I know MongoDB is able to handle a lot of requests/sec, but let's say I have to query a lot of documents of a collection given their _id; what sounds better: making a $in on the _id attribute with all the ids I want to get, or loop over findOne queries?
For comparison of different BSON type values, see the specified BSON comparison order. If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (for example, <value1> , <value2> , and so on).
MongoDB's latency increases together with the workload, up to the maximum throughput of roughly 1,000 operations per second.
MongoDB provides the find() that is used to find multiple values or documents from the collection. The find() method returns a cursor of the result set and prints all the documents. To find the multiple values, we can use the aggregation operations that are provided by MongoDB itself.
I would definitely go with using the $in query and providing a array of _ids.
Example:
db.collection.find({ "key": { "$in": [ ObjectId("xxx"), ObjectId("yyy"), ObjectId("zzz") ] } })
Why?
There's some additional documentation here if you want to check it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With