Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB select where in array of _id?

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?

like image 338
itsme Avatar asked Oct 10 '11 13:10

itsme


People also ask

How do I query an array of objects in MongoDB?

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.

How do I query multiple values in MongoDB?

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.

Where can I find nested documents in MongoDB?

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.

How do I test multiple conditions in MongoDB?

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.


1 Answers

Easy :)

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

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

like image 58
programmersbook Avatar answered Sep 22 '22 00:09

programmersbook