Introduction to MongoDB $not Operator

The $not operator is an operator in the MongoDB query language that is used to negate a query condition in a query. It can perform a logical NOT operation on a conditional expression, inverting the query result. In MongoDB, the $not operator can be used in any query statement that supports logical operations.

Syntax

The syntax format of the $not operator is as follows:

{ <field>: { $not: { <operator-expression> } } }

Where <field> represents the name of the field to be queried and <operator-expression> represents the query expression to be negated. It can be any expression that supports logical operations, such as $eq, $ne, $lt, $lte, $gt, $gte, etc.

Use Cases

The $not operator is commonly used to query results that do not meet certain conditions or to query documents where certain fields are not equal to a specific value. For example, the $not operator can be used to query documents where the age field is not equal to 18:

db.users.find({ age: { $not: { $eq: 18 } } })

Examples

Example 1: Querying documents where a field is not equal to a specific value

Consider a collection called grades that contains information on student scores. Each document contains the student’s name and math score. Suppose we want to query all student information where the math score is not equal to 90, we can use the $not operator for negation query:

db.grades.find({ math_score: { $not: { $eq: 90 } } })

The query result is as follows:

{ "_id" : ObjectId("61fc07de414a5a877d4de0d0"), "name" : "Tom", "math_score" : 85 }
{ "_id" : ObjectId("61fc07de414a5a877d4de0d2"), "name" : "Jack", "math_score" : 80 }
{ "_id" : ObjectId("61fc07de414a5a877d4de0d3"), "name" : "Lucy", "math_score" : 92 }
{ "_id" : ObjectId("61fc07de414a5a877d4de0d5"), "name" : "David", "math_score" : 88 }

As we can see, the query result only contains documents where the math score is not equal to 90.

Example 2: Querying documents where a field does not match a specific regular expression

Suppose we have a collection called books that contains information on many books, including the title, author, publisher, etc. Now we want to query all book information where the title does not contain the keyword “MongoDB”. We can use the $not operator and regular expression for the query:

db.books.find({ title: { $not: /MongoDB/ } })

The query results are as follows:

{ "_id": ObjectId("602cbef42dcf0c61f6e1a0a1"), "title": "The Art of Computer Programming", "author": "Donald Knuth", "publisher": "Addison-Wesley Professional", "year": 2011 }
{ "_id": ObjectId("602cbf162dcf0c61f6e1a0a2"), "title": "Introduction to Algorithms", "author": "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein", "publisher": "The MIT Press", "year": 2009 }
{ "_id": ObjectId("602cbf312dcf0c61f6e1a0a3"), "title": "Computer Networks", "author": "Andrew S. Tanenbaum and David J. Wetherall", "publisher": "Pearson Education", "year": 2011 }
{ "_id": ObjectId("602cbf3f2dcf0c61f6e1a0a4"), "title": "Design Patterns: Elements of Reusable Object-Oriented Software", "author": "Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides", "publisher": "Addison-Wesley Professional", "year": 1994 }

With this query statement, we successfully queried all the book information that does not include the keyword “MongoDB” in the title.

Conclusion

The $not operator can be used to query documents that do not meet specific conditions, and can be used together with other operators, which is very flexible. It should be noted that $not can only be used to query one condition. If you need to query the reverse result of multiple conditions, you need to use the $not operator.