admin管理员组文章数量:1414604
I have an string array of id's that i'd like to use with the find function.
dbpanies.find( { _id : { $in : arr} });
arr
looks something like this,
[ '563a2c60b511b7ff2c61e938', '563a2c60b511b7ff2c61e8b7' ];
I see from the documentation that ObjectID()
doesn't accept arrays. How can I search for a list of documents with this array? Will I have to recreate the array so that all elements are ObjectID's?
I have an string array of id's that i'd like to use with the find function.
db.panies.find( { _id : { $in : arr} });
arr
looks something like this,
[ '563a2c60b511b7ff2c61e938', '563a2c60b511b7ff2c61e8b7' ];
I see from the documentation that ObjectID()
doesn't accept arrays. How can I search for a list of documents with this array? Will I have to recreate the array so that all elements are ObjectID's?
- What is the output when you run that query? – TbWill4321 Commented Dec 15, 2015 at 22:31
1 Answer
Reset to default 7One option is to use the map
function to get the list of ObjectId
's from the list of string ids:
arr.map(function (id) {
return ObjectId(id);
})
Plugged into your query:
db.panies.find({_id: { $in: arr.map(function (id) {return ObjectId(id);})}})
本文标签: javascriptFind documents with array of string ID39s in MongoDBStack Overflow
版权声明:本文标题:javascript - Find documents with array of string ID's in MongoDB - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745193712a2647034.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论