简体   繁体   中英

MongoDB request by array of _ids (is it possible?)

For example I have n documents:

{ _id : 1 }
{ _id : 2 }
{ _id : 3 }
...
{ _id : n }

By some logic I got array of m _ids: [1, 30, 500, 1001, ..., i ];

Is it possible to make ONE request using array of _ids for retrieving Cursor of these m documents? I do not want to make separate requests like:

db.things.findOne({_id:1});

db.things.findOne({_id:30});

db.things.findOne({_id:500});

db.things.findOne({_id:1001});

...

db.things.findOne({_id:i});

And if it is possible, how do that on JavaScript Shell and with official C# driver?

Thank you!!!

c = db.things.find({_id:{$in:[1,30,500,1001,...,i]}})

And to do this with the C# Driver:

var documentIds = new BsonValue[] { ObjectId.Parse("id1"),  ObjectId.Parse("id2")}; 
var query = Query.In("_id", documentIds); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM