On this page本页内容
$and
¶Syntax: { $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
$and
performs a logical 对一个或多个表达式(例如AND
operation 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. <expression1>
、<expression2>
)的数组执行逻辑“与”运算,并选择满足数组中所有表达式的文档。The $and
operator uses short-circuit evaluation. $and
运算符使用短路评估。If the first expression (e.g. 如果第一个表达式(例如<expression1>
) evaluates to false
, MongoDB will not evaluate the remaining expressions.<expression1>
)的计算结果为false
,MongoDB将不会计算其余表达式。
Note
MongoDB provides an implicit 在指定以逗号分隔的表达式列表时,MongoDB提供了隐式AND
operation when specifying a comma separated list of expressions.AND
操作。
AND
Consider the following example:考虑下面的例子:
This query will select all documents in the 此查询将选择inventory
collection where:inventory
集合中的所有文档,其中:
price
field value is not equal to 1.99
andprice
字段值不等于1.99
,并且price
field exists.price
字段存在。This query can be also be constructed with an implicit 也可以通过组合AND
operation by combining the operator expressions for the price
field. price
字段的运算符表达式,使用隐式AND
操作构造此查询。For example, this query can be written as:例如,此查询可以编写为:
AND
Consider the following example:考虑下面的例子:
This query will select all documents where:此查询将选择所有文档,其中:
qty
field value is less than 10
or greater than 50
, andqty
字段值小于10
或大于50
,并且sale
field value is equal to true
or the price
field value is less than 5
.sale
字段值等于true
或price
字段值小于5
。This query cannot be constructed using an implicit 无法使用隐式AND
operation, because it uses the $or
operator more than once.AND
操作构造此查询,因为它多次使用$or
运算符。