MongoDBでは、$not 集計パイプライン演算子はブール値を評価し、反対のブール値を返します。
つまり、ブール値がtrueと評価された場合 、$not 演算子はfalseを返します 。そして、ブール値がfalseと評価されたとき 、$not 演算子はtrueを返します 。
例
testsというコレクションがあるとします。 次のドキュメントを使用:
{ "_id" : 1, "data" : true }
{ "_id" : 2, "data" : false }
$notを適用すると次のようになります dataに 各ドキュメントのフィールド:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1, 2 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) 結果:
{ "data" : true, "result" : false }
{ "data" : false, "result" : true }
$notであることがわかります 各ブール値の反対を返しました。
ゼロ、ヌル、および未定義の値
$not オペレーターは0を評価します 、null 、およびundefined falseとして –これは、trueを返すことを意味します 。
次のドキュメントがあるとします。
{ "_id" : 3, "data" : 0 }
{ "_id" : 4, "data" : null }
{ "_id" : 5, "data" : undefined }
$notを適用すると次のようになります :
db.test.aggregate(
[
{ $match: { _id: { $in: [ 3, 4, 5 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) 結果:
{ "data" : 0, "result" : true }
{ "data" : null, "result" : true }
{ "data" : undefined, "result" : true }
予想どおり、それらはすべてtrueを返しました (つまり、falseとして評価されました 。
その他すべての値
$not 他のすべての値をtrueとして演算子します –これは、falseを返すことを意味します 。
次のドキュメントがあるとします。
{ "_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") }
$notを適用すると次のようになります :
db.test.aggregate(
[
{ $match: { _id: { $in: [ 6, 7, 8, 9, 10 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) 結果:
{ "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 }
それらはすべてfalseを返します (つまり、trueと評価されます 。
欠落しているフィールド
$notを適用する 存在しないフィールドへの評価はfalse (つまり、trueを返します 。
次のドキュメントがあるとします。
{ "_id" : 11 }
そして、$notを適用します それに:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 11 ] } } },
{ $project: {
_id: 0,
data: 1,
result: { $not: [ "$data" ] } }
}
]
) 結果:
{ "result" : true } 別のオペレーターを否定する
$not 演算子を使用して、別の演算子のブール出力を無効にすることができます。
petsというコレクションがあるとします。 次のドキュメントを使用:
{ "_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 }
$notを使用できます たとえば、$gtと組み合わせて ウェイトフィールドを評価するには:
db.pets.aggregate(
[
{ $project: {
_id: 0,
name: 1,
weight: 1,
result: { $not: [ { $gt: [ "$weight", 100 ] } ] } }
}
]
) 結果:
{ "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 }
繰り返しになりますが、ここでも同じですが、今回は$gtのフィールドを出力します。 結果、および notのフィールド $gt 結果:
db.pets.aggregate(
[
{ $project: {
_id: 0,
weight: 1,
gt: { $gt: [ "$weight", 100 ] },
notGt: { $not: [ { $gt: [ "$weight", 100 ] } ] } }
}
]
) 結果:
{ "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 }
毎回$gt 結果としてtrue 、$not オペレーターがそれをfalseに切り替えました 。そして毎回$gt 結果としてfalse 、$not trueに反転しました 。