Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if multiple documents exist

Is there such a query that gets multiple fields, and returns which of these exists in the collection? For example, if the collection has only:

{id : 1}
{id : 2}

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

like image 887
shay__ Avatar asked Jun 16 '15 08:06

shay__


1 Answers

You are looking for the $in-operator.

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

This will get you any documents where the id-field (different from the special _id field) is 1 or 3. When you only want the values of the id field and not the whole documents:

db.collection.find({ id: { $in: [ 1, 3 ] } }, { _id: false, id:true });
like image 143
Philipp Avatar answered Sep 21 '22 04:09

Philipp