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

Viewed 8599

I'm just curious what's difference between .in() and .all() methods in mongoose Query? Can you explain by a simple example.

4 Answers
$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

Related