[ACCEPTED]-What's difference between .in() and all.() operators in mongoose?-mongoose

Accepted answer
Score: 21

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

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

For example, consider 6 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 5 return only the documents which contains 4 both cooking and singing skills as a set 3 ie Balaji and Ramesh.

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

Score: 19

Here is the explanation from mongodb.org:

$all

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

{ a: [ 1, 2, 3 ] }

would 7 be matched by

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

but not

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

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

Read more about mongodb 1 operators here

Score: 1
$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']},
]

0

Score: 0

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 16 - This an array operator that is equivalent 15 to an AND operation. This shall return documents 14 from a collection if it matches ALL VALUES in the specified array. The below 13 helps in determining all the documents that 12 have value 'Chess' 'Cricket' both.

db.gamesapp.find({"favourite_games":{$all:["Chess","Cricket"]}})

This 11 shall list the documents that is of ObjectId 7892de7a123be821ebcca757, 7892de7a123be821ebcca758 10 and 7892de7a123be821ebcca760

$in - This 9 is a comparison query operator which is 8 based on array that lists the possible conditions. It 7 helps in querying a variety of values It 6 lists documents that matches ANY OF THE values that exist in the 5 specified array in the query. The below helps in determining 4 all the documents that have values 'Chess' or 'Cricket'

db.gamesapp.find({"favourite_games":{$in:["Chess", "Cricket"]}})

This 3 shall list all the documents that is of 2 ObjectId 7892de7a123be821ebcca757 , 7892de7a123be821ebcca758, 7892de7a123be821ebcca759 1 and 7892de7a123be821ebcca760

More Related questions