$not (aggregation)
Nesta página
Definição
$not
Evaluates a boolean and returns the opposite boolean value; i.e. when passed an expression that evaluates to
true
,$not
returnsfalse
; when passed an expression that evaluates tofalse
,$not
returnstrue
.$not
tem a seguinte sintaxe:{ $not: [ <expression> ] } Para mais informações sobre expressões, consulte Operadores de Expressão.
Comportamento
Além do valor booleano false
, $not
avalia como false
os seguintes valores: null
, 0
e undefined
. O $not
avalia todos os outros valores como true
, incluindo valores numéricos diferentes de zero e arrays.
Exemplo | Resultado |
---|---|
|
|
|
|
|
|
|
|
|
|
Exemplo
Considere uma coleção inventory
com os seguintes documentos:
{ "_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
:
db.inventory.aggregate( [ { $project: { item: 1, result: { $not: [ { $gt: [ "$qty", 250 ] } ] } } } ] )
A operação retorna os seguintes resultados:
{ "_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 }