Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 511
Erik Avatar asked Aug 19 '12 15:08

Erik


People also ask

What is difference between $in and $All MongoDB?

$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.

What is $all in Mongoose?

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.

What does lean () do in Mongoose?

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.

How many operators are there in MongoDB?

23 Common MongoDB Operators & How To Use Them.


3 Answers

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

like image 190
udidu Avatar answered Oct 20 '22 15:10

udidu


$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.

like image 23
balajivijayan Avatar answered Oct 20 '22 15:10

balajivijayan


$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']},
]
like image 40
Chandrakesha Rao Avatar answered Oct 20 '22 15:10

Chandrakesha Rao