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?
To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.
MongoDB provides the find() that is used to find multiple values or documents from the collection. The find() method returns a cursor of the result set and prints all the documents. To find the multiple values, we can use the aggregation operations that are provided by MongoDB itself.
Accessing embedded/nested documents – In MongoDB, you can access the fields of nested/embedded documents of the collection using dot notation and when you are using dot notation, then the field and the nested field must be inside the quotation marks.
Find Multiple Conditions Using the $or Operator The or operator in MongoDB is used to select or retrieve only documents that match at least one of the provided phrases. You can also use this operator in a method like a find() , update() , etc., as per the requirements.
Easy :)
db.collection.find( { _id : { $in : [1,2,3,4] } } );
taken from: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With