$and¶
On this page
-
$and¶ Syntax:
{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }$andperforms a logicalANDoperation on an array of one or more expressions (e.g.<expression1>,<expression2>, etc.) and selects the documents that satisfy all the expressions in the array. The$andoperator uses short-circuit evaluation. If the first expression (e.g.<expression1>) evaluates tofalse, MongoDB will not evaluate the remaining expressions.Note
MongoDB provides an implicit
ANDoperation when specifying a comma separated list of expressions.
Examples¶
AND Queries With Multiple Expressions Specifying the Same Field¶
Consider the following example:
This query will select all documents in the inventory
collection where:
- the
pricefield value is not equal to1.99and - the
pricefield exists.
This query can be also be constructed with an implicit AND
operation by combining the operator expressions for the price
field. For example, this query can be written as:
AND Queries With Multiple Expressions Specifying the Same Operator¶
Consider the following example:
This query will select all documents where:
- the
qtyfield value is less than20or greater than50, and - the
salefield value is equal totrueor thepricefield value is less than5.
This query cannot be constructed using an implicit AND operation,
because it uses the $or operator more than once.