最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I retrieve multiple docs from Mongo by id? - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question asked Apr 20, 2012 at 12:24 fancyfancy 51.4k64 gold badges156 silver badges230 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

you need to use $in operator, that would give you desired result.

https://docs.mongodb.com/manual/reference/operator/query/in/

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()
发布评论

评论列表(0)

  1. 暂无评论