$and

在本页面

$and一个或多个表达式(例如<expression1><expression2>等)的数组执行逻辑AND运算,并选择满足所有数组中所有表达式的文档。 $and运算符使用短路评估。如果第一个表达式(例如<expression1>)求值为false,则 MongoDB 将不对其余表达式求值。

Note

指定表达式的逗号分隔列表时,MongoDB 提供隐式的AND操作。

Examples

AND 具有多个表达式的查询指定相同的字段

考虑以下示例:

db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )

此查询将选择inventory集合中的所有文档,其中:

通过组合price字段的运算符表达式,也可以使用隐式AND操作构造此查询。例如,此查询可以写为:

db.inventory.find( { price: { $ne: 1.99, $exists: true } } )

AND 具有多个表达式的查询指定相同的运算符

考虑以下示例:

db.inventory.find( {
    $and: [
        { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] },
        { $or: [ { sale: true }, { price : { $lt : 5 } } ] }
    ]
} )

该查询将选择以下位置的所有文档:

无法使用隐式AND操作构造此查询,因为它多次使用$or运算符。

See also

find(), update(), $ne, $exists, $set.

首页