MongoDB select where in array of _id?

Issue

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?

Solution

Easy 🙂

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

taken from: https://www.mongodb.com/docs/manual/reference/operator/query/in/#mongodb-query-op.-in

Answered By – programmersbook

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published