Reputation: 51383
I have an array of ids and I want to retrieve all of them at once. Is this possible, can I pass an array of ids somehow and have all of them returned to me? If so, how?
I'm using the node-native driver.
Thanks!
Upvotes: 13
Views: 8240
Reputation: 12930
Using the $in operator you can do something similar to:
const ids = ["123", "456","789"]
const items = await db
.collection("items")
.find({ "_id": { "$in": ids.map(id => new ObjectId(id)) } })
.toArray()
Upvotes: 0
Reputation: 43235
you need to use $in
operator, that would give you desired result.
https://docs.mongodb.com/manual/reference/operator/query/in/
Upvotes: 17