How to check for multiple documents

Is there a query that receives multiple fields and returns which one exists in the collection? For example, if a collection has only:

{id : 1}
{id : 2}

And I want to know which one [{id : 1} , {id : 3}]exists in it, then the result will be similar to [{id : 1}].

+4
source share
2 answers

You are looking for $ in-operator .

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

This will give you any documents where the id field (other than the special field _id) is 1 or 3. When you need only the id field values, and not all documents:

db.collection.find({ id: { $in: [ 1, 3 ] } }, { _id: false, id:true });
+7
source

, key with value , , $or.

id _id .

$ , :

db.collection.find({$or:[{"id":1},{"id":3}]},{"_id":0,"id":1}) 

_id, :

db.collection.find({$or:[{"_id":ObjectId("557fda78d077e6851e5bf0d3")},{"_id":ObjectId("557fda78d077e6851e5bf0d5")}]}
+1

All Articles