MongoDB select where in array of _id?

2019-01-16 13:39发布

问题:

is possible in mongo db to select collection's documents like in SQL :

SELECT * FROM collection WHERE _id IN (1,2,3,4);

or if i have a _id array i must select one by one and then recompose the array/object of results?

回答1:

Easy :)

db.collection.find( { _id : { $in : [1,2,3,4] } } );

taken from: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in



回答2:

In this code list is the array of ids in user collection

var list = ["5883d387971bb840b7399130","5883d389971bb840b7399131","5883d38a971bb840b7399132"]

    .find({ _id: {$in : list}})