On this page本页内容
$not
Evaluates a boolean and returns the opposite boolean value; i.e. when passed an expression that evaluates to 计算布尔值并返回相反的布尔值;例如,当传递一个计算结果为true, $not returns false; when passed an expression that evaluates to false, $not returns true.true的表达式时,$not返回false;当传递的表达式计算结果为false时,$not返回true。
$not has the following syntax:语法如下:
{ $not: [ <expression> ] }
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
In addition to the 除了false boolean value, $not evaluates as false the following: null, 0, and undefined values. false布尔值之外,$not在以下值中的计算结果为false:null、0和undefined的值。The $not evaluates all other values as true, including non-zero numeric values and arrays.$not将所有其他值计算为true,包括非零数值和数组。
{ $not: [ true ] } | false |
{ $not: [ [ false ] ] } | false |
{ $not: [ false ] } | true |
{ $not: [ null ] } | true |
{ $not: [ 0 ] } | true |
Consider an 考虑使用以下文档进行inventory collection with the following documents:inventory集合:
{ "_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 }
The following operation uses the 以下操作使用$not operator to determine if qty is not greater than 250:$not运算符确定数量是否不大于250:
db.inventory.aggregate(
[
{
$project:
{
item: 1,
result: { $not: [ { $gt: [ "$qty", 250 ] } ] }
}
}
]
)
The operation returns the following results:该操作返回以下结果:
{ "_id" : 1, "item" : "abc1", "result" : false }
{ "_id" : 2, "item" : "abc2", "result" : true }
{ "_id" : 3, "item" : "xyz1", "result" : true }
{ "_id" : 4, "item" : "VWZ1", "result" : false }
{ "_id" : 5, "item" : "VWZ2", "result" : true }