W MongoDB $not Operator potoku agregacji oblicza wartość logiczną i zwraca przeciwną wartość logiczną.
Innymi słowy, kiedy wartość logiczna daje wartość true , $not operator zwraca false . A kiedy wartość logiczna daje wartość false , $not operator zwraca true .
Przykład
Załóżmy, że mamy kolekcję o nazwie tests z następującymi dokumentami:
{ "_id" : 1, "data" : true }
{ "_id" : 2, "data" : false }
Oto, co się dzieje, gdy zastosujemy $not do data pole każdego dokumentu:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1, 2 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) Wynik:
{ "data" : true, "result" : false }
{ "data" : false, "result" : true }
Widzimy, że $not zwrócił przeciwieństwo każdej wartości logicznej.
Wartości zero, zero i niezdefiniowane
$not operator ocenia 0 , null i undefined jako false – co oznacza, że zwraca true .
Załóżmy, że mamy następujące dokumenty:
{ "_id" : 3, "data" : 0 }
{ "_id" : 4, "data" : null }
{ "_id" : 5, "data" : undefined }
Oto, co się dzieje, gdy zastosujemy $not :
db.test.aggregate(
[
{ $match: { _id: { $in: [ 3, 4, 5 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) Wynik:
{ "data" : 0, "result" : true }
{ "data" : null, "result" : true }
{ "data" : undefined, "result" : true }
Zgodnie z oczekiwaniami wszystkie zwróciły true (co oznacza, że zostały ocenione jako false .
Wszystkie inne wartości
$not operator wszystkie inne wartości jako true – co oznacza, że zwraca false .
Załóżmy, że mamy następujące dokumenty:
{ "_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") }
Oto, co się dzieje, gdy zastosujemy $not :
db.test.aggregate(
[
{ $match: { _id: { $in: [ 6, 7, 8, 9, 10 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) Wynik:
{ "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 }
Wszystkie zwracają false (co oznacza, że oceniają jako true ).
Brakujące pola
Stosowanie $not do pola, które nie istnieje, ocenia jako false (co oznacza, że zwraca true ).
Załóżmy, że mamy następujący dokument:
{ "_id" : 11 }
I stosujemy $not do tego:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 11 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) Wynik:
{ "result" : true } Zanegować innego operatora
$not Operator może być użyty do zanegowania wyjścia logicznego innego operatora.
Załóżmy, że mamy kolekcję o nazwie pets z następującymi dokumentami:
{ "_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 }
Możemy użyć $not w połączeniu z powiedzmy $gt aby ocenić pole wagi:
db.pets.aggregate(
[
{ $project: {
_id: 0,
name: 1,
weight: 1,
result: { $not: [ { $gt: [ "$weight", 100 ] } ] } }
}
]
) Wynik:
{ "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 }
Powtórzę to ponownie, tutaj jest ponownie, ale tym razem wypisujemy pole dla $gt wyniki, a także pole nie $gt wyniki:
db.pets.aggregate(
[
{ $project: {
_id: 0,
weight: 1,
gt: { $gt: [ "$weight", 100 ] },
notGt: { $not: [ { $gt: [ "$weight", 100 ] } ] } }
}
]
) Wynik:
{ "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 }
Za każdym razem $gt spowodowało true , $not operator przerzucił go na false . I za każdym razem $gt spowodowało false , $not odwrócił go do true .