$and¶
-
$and¶ New in version 2.0.
Syntax:
{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }$andperforms a logicalANDoperation on an array of two 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.Consider the following example:
This query will select all documents in the
inventorycollection where:pricefield value equals1.99andqtyfield value is less than20andsalefield value is equal totrue.
MongoDB provides an implicit
ANDoperation when specifying a comma separated list of expressions. For example, you may write the above query as:If, however, a query requires an
ANDoperation on the same field such as{ price: { $ne: 1.99 } } AND { price: { $exists: true } }, then either use the$andoperator for the two separate expressions or combine the operator expressions for the field{ price: { $ne: 1.99, $exists: true } }.Consider the following examples:
Both
update()operations will set the value of theqtyfield in documents where:- the
pricefield value does not equal1.99and - the
pricefield exists.