I'm just curious what's difference between .in() and .all() methods in mongoose Query? Can you explain by a simple example.
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.
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 - 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']},
]
Consider the following collection 'gamesapp'
{
_id: ObjectId("7892de7a123be821ebcca757"),
name: "kid1",
games: [ "Chess", "Cricket", "Football", "Hockey", "Carrom" ],
favourite_games: [ "Chess", "Cricket", "Football" ]
}
{
_id: ObjectId("7892de7a123be821ebcca758"),
name: "kid2",
games: [ "Chess", "Cricket", "Football", "Hockey", "Carrom" ],
favourite_games: [ "Chess", "Cricket" ]
}
{
_id: ObjectId("7892de7a123be821ebcca759"),
name: "kid3",
games: [ "Chess", "Cricket", "Football", "Hockey", "Carrom" ],
favourite_games: [ "Chess" ]
}
{
_id: ObjectId("7892de7a123be821ebcca760"),
name: "kid4",
games: [ "Chess", "Cricket", "Football", "Hockey", "Carrom" ],
favourite_games: [ "Chess", "Cricket" ]
}
$all - This an array operator that is equivalent to an AND operation. This shall return documents from a collection if it matches ALL VALUES in the specified array. The below helps in determining all the documents that have value 'Chess' 'Cricket' both.
db.gamesapp.find({"favourite_games":{$all:["Chess","Cricket"]}})
This shall list the documents that is of ObjectId 7892de7a123be821ebcca757, 7892de7a123be821ebcca758 and 7892de7a123be821ebcca760
$in - This is a comparison query operator which is based on array that lists the possible conditions. It helps in querying a variety of values It lists documents that matches ANY OF THE values that exist in the specified array in the query. The below helps in determining all the documents that have values 'Chess' or 'Cricket'
db.gamesapp.find({"favourite_games":{$in:["Chess", "Cricket"]}})
This shall list all the documents that is of ObjectId 7892de7a123be821ebcca757 , 7892de7a123be821ebcca758, 7892de7a123be821ebcca759 and 7892de7a123be821ebcca760
I have used the $size
to check the length of the objects to search excat match opertion.
db.getCollection("testCollection").find({
"status": true,
"attributes": {
"$size":6, // Ensure the array has exactly 6 elements
"$all": [
{ "key": "key0", "value": "1" },
{ "key": "key1", "value": "N" }
]
}
});
This will be excat match.
© 2022 - 2024 — McMap. All rights reserved.