I'm just curious what's difference between .in() and .all() methods in mongoose Query? Can you explain by a simple example.
$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.
The $all operator allows you to check for documents that have an array field with all of the values given. The syntax is very straightforward and is easy to use.
The lean() function tells mongoose to not hydrate query results. In other words, the results of your queries will be the same plain JavaScript objects that you would get from using the Node. js MongoDB driver directly, with none of the mongoose magic.
23 Common MongoDB Operators & How To Use Them.
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.
$all - This an array operator that is equivalent to an $and operation.
[
{name:'Shubham',hobbies:['cricket','dancing','football']},
{name:'Chandrakesha',hobbies:['cricket','singing','dancing','football']},
{name:'Nitish',hobbies:['cricket','singing','dancing','football']},
{name:'Lovish',hobbies:['readingbooks','singing','dancing','football']},
]
Query:->
{hobbies: { $all : ["readingbooks", "singing" ] } }
OR
can write like below query
{
$and: [
{"hobbies": "readingbooks"},
{"hobbies": "singing"},
]
}
will return only the documents which contains both readingbooks and singing hobbies
output:->
[
{name:'Lovish',hobbies:['readingbooks','singing','dancing','football']},
]
-------------------------------
$in operator retrieves all the documents which contains the either of the values we pass.
[
{name:'Shubham',hobbies:['cricket','dancing','football']},
{name:'Chandrakesha',hobbies:['cricket','singing','dancing','football']},
{name:'Nitish',hobbies:['cricket','singing','dancing','football']},
{name:'Lovish',hobbies:['readingbooks','singing','dancing','football']},
]
Query:->
{hobbies: { $in : ["readingbooks", "singing" ] } }
OR
can write like below query
{
$or: [
{"hobbies": "readingbooks"},
{"hobbies": "singing"},
]
}
will return all the documents which contains any of them readingbooks or singing hobbies
output:->
[
{name:'Chandrakesha',hobbies:['cricket','singing','dancing','football']},
{name:'Nitish',hobbies:['cricket','singing','dancing','football']},
{name:'Lovish',hobbies:['readingbooks','singing','dancing','football']},
]
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