javascript - Find documents with array of string ID's in MongoDB - Stack Overflow

I have an string array of id's that i'd like to use with the find function.dbpanies.find( { _

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?

Share Improve this question asked Dec 15, 2015 at 22:22 AppTestAppTest 5011 gold badge7 silver badges25 bronze badges 1
  • What is the output when you run that query? – TbWill4321 Commented Dec 15, 2015 at 22:31
Add a ment  | 

1 Answer 1

Reset to default 7

One 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);})}})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745193712a4615987.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信