MongoDB AND operator ( $and )

MongoDB provides different types of logical query operators and $and operator is one of them. This operator is used to perform logical AND operation on the array of one or more expressions and select or retrieve only those documents that match all the given expression in the array. You can use this operator in methods like find(), update(), etc. according to your requirements.

  • This operator performs short-circuit evaluation.
  • If the first expression of $and operator evaluates to false, then MongoDB will not evaluate the remaining expressions in the array.
  • You can also use AND operation implicitly with the help of comma(, ).
  • You can use AND operation explixtly (i.e., $and) when the same field or operator specified in multiple expressions.

Syntax:

{ $and: [ { Expression1 }, { Expression2 }, ..., { ExpressionN } ] }
or
{ { Expression1 }, { Expression2 }, ..., { ExpressionN }}

Examples:
In the following examples, we are working with:

Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form of field-value pairs.



Matching values using $and operator:

In this example, we are retrieving only those employee’s documents whose branch is CSE and joiningYear is 2018.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({$and: [{branch: "CSE"}, 
                            {joiningYear: 2018}]}).pretty()

chevron_right


Using $and operator with multiple expressions specifying the same field :

In this example, we are retrieving only those employee’s documents whose branch is CSE.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({$and: [{branch: {$eq: "CSE"}}, 
                            {branch: {$exists: true}}]}).pretty()

chevron_right


or

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({branch: {$eq: "CSE", $exists: true}}).pretty()

chevron_right


Using $and operator with multiple expressions specifying the same operator :

In this example, we are retrieving only those employee’s documents whose satisfy the given conditions.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({$and: [{$or: [{branch: "ECE"}, {joiningYear: 2017}]}, 
                            {$or: [{"personal.state": "UP"}, 
                                   {"personal.age": 25}]}]}).pretty()

chevron_right



Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.