I MongoDB, $not aggregeringspipeline-operatören utvärderar ett booleskt värde och returnerar det motsatta booleska värdet.
Med andra ord, när boolean utvärderas till true , $not operatorn returnerar false . Och när boolean utvärderas till false , $not operatorn returnerar true .
Exempel
Anta att vi har en samling som heter tests med följande dokument:
{ "_id" : 1, "data" : true }
{ "_id" : 2, "data" : false }
Det här är vad som händer när vi tillämpar $not till data fält för varje dokument:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1, 2 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) Resultat:
{ "data" : true, "result" : false }
{ "data" : false, "result" : true }
Vi kan se att $not returnerade motsatsen till varje boolean.
Noll-, noll- och odefinierade värden
$not operatören utvärderar 0 , null och undefined som false – vilket betyder att den returnerar true .
Anta att vi har följande dokument:
{ "_id" : 3, "data" : 0 }
{ "_id" : 4, "data" : null }
{ "_id" : 5, "data" : undefined }
Det här är vad som händer när vi tillämpar $not :
db.test.aggregate(
[
{ $match: { _id: { $in: [ 3, 4, 5 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) Resultat:
{ "data" : 0, "result" : true }
{ "data" : null, "result" : true }
{ "data" : undefined, "result" : true }
Som förväntat returnerade de alla true (vilket betyder att de utvärderades som false .
Alla andra värden
$not operatorn alla andra värden som true – vilket betyder att den returnerar false .
Anta att vi har följande dokument:
{ "_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") }
Det här är vad som händer när vi tillämpar $not :
db.test.aggregate(
[
{ $match: { _id: { $in: [ 6, 7, 8, 9, 10 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) Resultat:
{ "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 }
De returnerar alla false (vilket betyder att de utvärderar till true ).
Fält saknas
Använder $not till ett fält som inte finns utvärderas till false (vilket betyder att den returnerar true ).
Anta att vi har följande dokument:
{ "_id" : 11 }
Och vi tillämpar $not till det:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 11 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) Resultat:
{ "result" : true } Negera en annan operatör
$not operator kan användas för att negera den booleska utdata från en annan operator.
Anta att vi har en samling som heter pets med följande dokument:
{ "_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 }
Vi kan använda $not i samband med säg, $gt för att utvärdera viktfältet:
db.pets.aggregate(
[
{ $project: {
_id: 0,
name: 1,
weight: 1,
result: { $not: [ { $gt: [ "$weight", 100 ] } ] } }
}
]
) Resultat:
{ "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 }
För att upprepa detta, här är det igen, men den här gången matar vi ut ett fält för $gt resultat, samt fältet för inte $gt resultat:
db.pets.aggregate(
[
{ $project: {
_id: 0,
weight: 1,
gt: { $gt: [ "$weight", 100 ] },
notGt: { $not: [ { $gt: [ "$weight", 100 ] } ] } }
}
]
) Resultat:
{ "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 }
Varje gång $gt resulterade i true , $not operatören vände den till false . Och varje gång $gt resulterade i false , $not vände den till true .