在本页面
例子
$and
语法:{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
$and执行的逻辑AND的阵列上操作_的一个或多个_表达式(例如<expression1>, <expression2>等),并且选择满足该文件 的所有_阵列中的表达式。$and运算符使用_短路计算。如果第一个表达式(例如<expression1>)的计算结果为false,则MongoDB将不计算其余的表达式。
AND
<expression1>
<expression2>
false
注意AND当指定逗号分隔的表达式列表时,MongoDB提供隐式操作。
注意
AND当指定逗号分隔的表达式列表时,MongoDB提供隐式操作。
考虑以下示例:
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
此查询将选择inventory 集合中的所有文档,其中:
inventory
price字段值不等于1.99 与
price
1.99
price字段存在。
AND 通过组合price 字段的运算符表达式,也可以使用隐式操作构造此查询。例如,此查询可以写为:
db.inventory.find( { price: { $ne: 1.99, $exists: true } } )
该查询将选择以下位置的所有文档:
qty字段值小于20或大于50,和
qty
20
50
sale字段值是等于true 或所述price 字段值小于5。
sale
true
5
无法使用隐式AND操作构造此查询,因为它$or多次使用运算符。
$or
也可以看看find(),update(), $ne,$exists,$set。
也可以看看
find(),update(), $ne,$exists,$set。
find()
update()
$ne
$exists
$set
译者:李冠飞
校对:
最后更新于 4年前
db.inventory.find( { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } )