En MongoDB, el $not
El operador de canalización de agregación evalúa un valor booleano y devuelve el valor booleano opuesto.
En otras palabras, cuando el booleano se evalúa como true
, el $not
el operador devuelve false
. Y cuando el booleano se evalúa como false
, el $not
el operador devuelve true
.
Ejemplo
Supongamos que tenemos una colección llamada tests
con los siguientes documentos:
{ "_id" : 1, "data" : true } { "_id" : 2, "data" : false }
Esto es lo que sucede cuando aplicamos $not
a los data
campo de cada documento:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1, 2 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
)
Resultado:
{ "data" : true, "result" : false } { "data" : false, "result" : true }
Podemos ver que $not
devolvió el opuesto de cada valor booleano.
Valores cero, nulos e indefinidos
El $not
el operador evalúa 0
, null
y undefined
como false
– lo que significa que devuelve true
.
Supongamos que tenemos los siguientes documentos:
{ "_id" : 3, "data" : 0 } { "_id" : 4, "data" : null } { "_id" : 5, "data" : undefined }
Esto es lo que sucede cuando aplicamos $not
:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 3, 4, 5 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
)
Resultado:
{ "data" : 0, "result" : true } { "data" : null, "result" : true } { "data" : undefined, "result" : true }
Como era de esperar, todos devolvieron true
(lo que significa que evaluaron como false
.
Todos los demás valores
El $not
operador todos los demás valores como true
– lo que significa que devuelve false
.
Supongamos que tenemos los siguientes documentos:
{ "_id" : 6, "data" : [ true ] } { "_id" : 7, "data" : [ false ] } { "_id" : 8, "data" : 5 } { "_id" : 9, "data" : "Bat" } { "_id" : 10, "data" : ISODate("2021-01-03T23:30:15.100Z") }
Esto es lo que sucede cuando aplicamos $not
:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 6, 7, 8, 9, 10 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
)
Resultado:
{ "data" : [ true ], "result" : false } { "data" : [ false ], "result" : false } { "data" : 5, "result" : false } { "data" : "Bat", "result" : false } { "data" : ISODate("2021-01-03T23:30:15.100Z"), "result" : false }
Todos devuelven false
(lo que significa que se evalúan como true
).
Campos faltantes
Aplicando $not
a un campo que no existe se evalúa como false
(lo que significa que devuelve true
).
Supongamos que tenemos el siguiente documento:
{ "_id" : 11 }
Y aplicamos $not
a eso:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 11 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
)
Resultado:
{ "result" : true }
Negar otro operador
El $not
El operador puede usarse para negar la salida booleana de otro operador.
Supongamos que tenemos una colección llamada pets
con los siguientes documentos:
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 } { "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 } { "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 } { "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 } { "_id" : 5, "name" : "Bruce", "type" : "Kangaroo", "weight" : 100 } { "_id" : 6, "name" : "Hop", "type" : "Kangaroo", "weight" : 130 } { "_id" : 7, "name" : "Punch", "type" : "Kangaroo", "weight" : 200 } { "_id" : 8, "name" : "Snap", "type" : "Cat", "weight" : 12 } { "_id" : 9, "name" : "Ruff", "type" : "Dog", "weight" : 30 }
Podemos usar $not
junto con digamos, $gt
para evaluar el campo de peso:
db.pets.aggregate(
[
{ $project: {
_id: 0,
name: 1,
weight: 1,
result: { $not: [ { $gt: [ "$weight", 100 ] } ] } }
}
]
)
Resultado:
{ "name" : "Wag", "weight" : 20, "result" : true } { "name" : "Bark", "weight" : 10, "result" : true } { "name" : "Meow", "weight" : 7, "result" : true } { "name" : "Scratch", "weight" : 8, "result" : true } { "name" : "Bruce", "weight" : 100, "result" : true } { "name" : "Hop", "weight" : 130, "result" : false } { "name" : "Punch", "weight" : 200, "result" : false } { "name" : "Snap", "weight" : 12, "result" : true } { "name" : "Ruff", "weight" : 30, "result" : true }
Para reiterar esto, aquí está de nuevo, pero esta vez mostramos un campo para el $gt
resultados, así como el campo para no $gt
resultados:
db.pets.aggregate(
[
{ $project: {
_id: 0,
weight: 1,
gt: { $gt: [ "$weight", 100 ] },
notGt: { $not: [ { $gt: [ "$weight", 100 ] } ] } }
}
]
)
Resultado:
{ "weight" : 20, "gt" : false, "notGt" : true } { "weight" : 10, "gt" : false, "notGt" : true } { "weight" : 7, "gt" : false, "notGt" : true } { "weight" : 8, "gt" : false, "notGt" : true } { "weight" : 100, "gt" : false, "notGt" : true } { "weight" : 130, "gt" : true, "notGt" : false } { "weight" : 200, "gt" : true, "notGt" : false } { "weight" : 12, "gt" : false, "notGt" : true } { "weight" : 30, "gt" : false, "notGt" : true }
Cada vez $gt
resultó en true
, el $not
el operador lo cambió a false
. Y cada vez $gt
resultó en false
, $not
lo cambió a true
.