MongoDB $not Aggregation Pipeline Operator

In MongoDB, the $not aggregation pipeline operator evaluates a boolean and returns the opposite boolean value.

In other words, when the boolean evaluates to true, the $not operator returns false. And when the boolean evaluates to false, the $not operator returns true.

Example

Suppose we have a collection called tests with the following documents:

{ "_id" : 1, "data" : true }
{ "_id" : 2, "data" : false }

Here’s what happens when we apply $not to the data field of each document:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2 ] } } },
     { $project: { 
        _id: 0,
        data: 1,
        result: { $not: [ "$data" ] } } 
         }
   ]
)

Result:

{ "data" : true, "result" : false }
{ "data" : false, "result" : true }

We can see that $not returned the opposite of each boolean.

Zero, Null, and Undefined Values

The $not operator evaluates 0, null, and undefined as false – which means that it returns true.

Suppose we have the following documents:

{ "_id" : 3, "data" : 0 }
{ "_id" : 4, "data" : null }
{ "_id" : 5, "data" : undefined }

Here’s what happens when we apply $not:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 3, 4, 5 ] } } },
     { $project: { 
        _id: 0,
        data: 1,
        result: { $not: [ "$data" ] } } 
         }
   ]
)

Result:

{ "data" : 0, "result" : true }
{ "data" : null, "result" : true }
{ "data" : undefined, "result" : true }

As expected, they all returned true (which means that they evaluated as false.

All Other Values

The $not operator all other values as true – which means that it returns false.

Suppose we have the following documents:

{ "_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") }

Here’s what happens when we apply $not:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 6, 7, 8, 9, 10 ] } } },
     { $project: { 
        _id: 0,
        data: 1,
        result: { $not: [ "$data" ] } } 
         }
   ]
)

Result:

{ "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 }

They all return false (which means that they evaluate to true).

Missing Fields

Applying $not to a field that doesn’t exist evaluates to false (which means it returns true).

Suppose we have the following document:

{ "_id" : 11 }

And we apply $not to that:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 11 ] } } },
     { $project: { 
        _id: 0,
        data: 1,
        result: { $not: [ "$data" ] } } 
         }
   ]
)

Result:

{ "result" : true }

Negate Another Operator

The $not operator can be used to negate the boolean output of another operator.

Suppose we have a collection called pets with the following documents:

{ "_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 }

We can use $not in conjunction with say, $gt to evaluate the weight field:

db.pets.aggregate(
   [
     { $project: { 
        _id: 0,
        name: 1,
        weight: 1,
        result: { $not: [ { $gt: [ "$weight", 100 ] } ] } } 
         }
   ]
)

Result:

{ "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 }

To reiterate this, here it is again but this time we output a field for the $gt results, as well as the field for the not $gt results:

db.pets.aggregate(
   [
     { $project: { 
        _id: 0,
        weight: 1,
        gt: { $gt: [ "$weight", 100 ] },
        notGt: { $not: [ { $gt: [ "$weight", 100 ] } ] } } 
         }
   ]
)

Result:

{ "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 }

Each time $gt resulted in true, the $not operator flipped it to false. And each time $gt resulted in false, $not flipped it to true.