MongoDB select where in array of _id?

itsme picture itsme · Oct 10, 2011 · Viewed 73.6k times · Source

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?

Answer

programmersbook picture programmersbook · Oct 10, 2011

Easy :)

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

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