MongoDB select where in array of _id?
Easy :)
db.collection.find( { _id : { $in : [1,2,3,4] } } );
taken from: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in
Because mongodb uses bson
and for bson is important attribute types. and because _id
is ObjectId
you must use like this:
db.collection.find( { _id : { $in : [ObjectId('1'),ObjectId('2')] } } );
and in mongodb compass
use like this:
{ "_id" : { $in : [ObjectId('1'),ObjectId('2')] } }
Note: objectId in string has 24
length.
In this code list is the array of ids in user collection
var list = ["5883d387971bb840b7399130","5883d389971bb840b7399131","5883d38a971bb840b7399132"]
.find({ _id: {$in : list}})
You can try this
var ids = ["5883d387971bb840b7399130","5883d389971bb840b7399131","5883d38a971bb840b7399132"];
var oids = [];
ids.forEach(function(item){
oids.push(new ObjectId(item));
});
.find({ _id: {$in : oids}})