$and (aggregation)

On this page本页内容

Definition定义

$and

Evaluates one or more expressions and returns true if all of the expressions are true or if run with no argument expressions. 计算一个或多个表达式,如果所有表达式都为true,或者运行时没有参数表达式,则返回trueOtherwise, $and returns false.否则,$andfalse

$and syntax:

{ $and: [ <expression1>, <expression2>, ... ] }

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

In addition to the false boolean value, $and evaluates as false the following: null, 0, and undefined values. 除了false布尔值,$and将以下值:null0undefined评估为falseThe $and evaluates all other values as true, including non-zero numeric values and arrays.$and将所有其他值计算为true,包括非零数值和数组。

ExampleResult
{ $and: [ 1, "green" ] }true
{ $and: [ ] }true
{ $and: [ [ null ], [ false ], [ 0 ] ] }true
{ $and: [ null, true ] }false
{ $and: [ 0, true ] }false

Error Handling错误处理

To allow the query engine to optimize queries, $and handles errors as follows:要允许查询引擎优化查询,$and并按如下方式处理错误:

  • If any expression supplied to $and would cause an error when evaluated alone, the $and containing the expression may cause an error but an error is not guaranteed.如果提供给$and的任何表达式在单独计算时会导致错误,则包含该表达式的$and可能会导致错误但不能保证错误。
  • An expression supplied after the first expression supplied to $and may cause an error even if the first expression evaluates to false.在提供给$and的第一个表达式之后提供的表达式,即使第一个表达式的计算结果为false,也可能导致错误。

For example, the following query always produces an error if $x is 0:例如,如果$x0,则以下查询总是产生错误:

db.example.find( {
   $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] }
} )

The following query, which contains multiple expressions supplied to $and, may produce an error if there is any document where $x is 0:以下查询包含提供给$and的多个表达式,如果存在$x0的任何文档,则可能会产生错误:

db.example.find( {
   $and: [
      { x: { $ne: 0 } },
      { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } }
   ]
} )

Example示例

Create an example inventory collection with these documents:使用以下文档创建示例inventory集合:

db.inventory.insertMany([
   { "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 },
   { "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 },
   { "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 },
   { "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 },
   { "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
])

This operation uses the $and operator to determine if qty is greater than 100 and less than 250:此操作使用$and运算符确定qty是否大于100且小于250

db.inventory.aggregate(
   [
     {
       $project:
          {
            item: 1,
            qty: 1,
            result: { $and: [ { $gt: [ "$qty", 100 ] }, { $lt: [ "$qty", 250 ] } ] }
          }
     }
   ]
)

The operation returns these results:该操作返回以下结果:

{ "_id" : 1, "item" : "abc1", "qty" : 300, "result" : false }
{ "_id" : 2, "item" : "abc2", "qty" : 200, "result" : true }
{ "_id" : 3, "item" : "xyz1", "qty" : 250, "result" : false }
{ "_id" : 4, "item" : "VWZ1", "qty" : 300, "result" : false }
{ "_id" : 5, "item" : "VWZ2", "qty" : 180, "result" : true }
←  $allElementsTrue (aggregation)$anyElementTrue (aggregation) →