MongoDB NOT operator ( $not ) (original) (raw)

Last Updated : 5 May, 2026

The MongoDB $not operator negates a query condition and returns documents that do not match the specified expression, helping exclude unwanted results in complex filters.

Syntax

{ field: { $not: { } } }

Examples of Using $not Operator in MongoDB

In the following examples, we are working with:

Screenshot-2026-02-09-171004

Example 1: Matching values using $not operator

Retrieving only those contributor’s documents whose salary is not greater than 2000.

**Query:

db.contributor.find({salary: {$not: {$gt: 2000}}})

**Output:

Screenshot-2026-02-09-172002

Example 2: Matching values in nested/embedded documents using $not operator

Retrieving only those contributor’s documents whose age is not equal to 24.

**Query:

db.contributor.find({"personal.age": {$not: {$eq: 24}}})

**Output:

Screenshot-2026-02-09-172217

Example 3: Matching values in an array using $not operator

Retrieving only those contributor’s documents where the language is not Java or Perl.

**Query:

db.contributor.find({ language: {$not: {$in: ["Java", "Perl"]}}})

**Output:

Screenshot-2026-02-09-172349

Behavior of $not with Different Data Types

The MongoDB $not operator works across different data types, but its behavior can vary with arrays and missing fields, sometimes producing unexpected results.

Best Practices for Using the $not Operator

Use $not carefully with clear conditions to avoid performance issues and unexpected results.