What's difference between .in() and all.() operators in mongoose?

Here is the explanation from mongodb.org:

$all

The $all operator is similar to $in, but instead of matching any value in the specified array all values in the array must be matched. For example, the object

{ a: [ 1, 2, 3 ] }

would be matched by

db.things.find( { a: { $all: [ 2, 3 ] } } );

but not

db.things.find( { a: { $all: [ 2, 3, 4 ] } } );

An array can have more elements than those specified by the $all criteria. $all specifies a minimum set of elements that must be matched.

Read more about mongodb operators here


$all operator retrieves all the documents which contains the subset of the values we pass. The subset might be in any order.

$in operator retrieves all the documents which contains the either of the values we pass.

For example, consider the collection "skills" with following documents:

{ "Name" : "Balaji", "skills" : [ "Dancing", "Cooking", "Singing" ] }
{ "Name" : "Ramesh", "skills" : [ "Cooking", "Singing" ] }
{ "Name" : "Suresh", "skills" : [ "Dancing", "Singing" ] }

db.skills.find({skills: { $all : ["Cooking", "Singing" ] } } ) will return only the documents which contains both cooking and singing skills as a set ie Balaji and Ramesh.

db.skills.find({skills: { $in : ["Cooking", "Singing" ] } } ) will return all the documents since all documents contains either cooking or singing.