MongoDB NOR Operator ( $nor )
MongoDB provides various logical query operators to enable advanced querying capabilities, and one of the most important among them is the $nor
operator. This operator performs a logical NOR operation, allowing us to find documents that do not meet the conditions specified in multiple expressions.
In this article, we will explore the MongoDB $nor
operator in detail, explain its syntax, provide practical examples, and show how we can use it effectively to filter documents based on multiple conditions.
What is the MongoDB $nor
Operator?
The $nor
operator is used to combine multiple conditions and retrieve documents where none of the specified conditions are true. It performs the logical inverse of the $or
operator. Essentially, $nor
returns documents that fail to match all the given conditions in the provided array. This makes the $nor
operator highly useful for scenarios where we want to exclude documents that match any of a given set of conditions.
Syntax:
{ $nor: [ { Expression1 }, { Expression2 }, ... { ExpressionN } ] }
- Expression1, Expression2, ..., ExpressionN: These are conditions or queries that documents must not match. The operator returns documents where none of these conditions are true.
Examples of using MongoDB Operations
In the following examples, we are working with:
- Database: GeeksforGeeks
- Collection: contributor
- Document: three documents that contain the details of the contributors in the form of field-value pairs.

Example 1: Matching Values using $nor
Operator
In this example, we will retrieve documents where the salary is not 3000 and the branch is not "ECE". Using the $nor
operator, the query looks like this:
Query:
db.contributor.find({$nor: [{salary: 3000}, {branch: "ECE"}]}).pretty()
Output

Explanation:
This query excludes documents where either the salary is 3000 or the branch is "ECE". Only documents that satisfy both conditions (i.e., salary is not 3000 and branch is not "ECE") will be returned.
Example 2: Matching Values in Nested/Embedded Documents using $nor
Operator
MongoDB allows for the use of embedded or nested documents, which means you can query fields inside subdocuments. In this example, we retrieve employee documents where the age is not 24 and the state is not "AP" in the personal
subdocument:
Query:
db.contributor.find({$nor: [{"personal.age": 24},
{"personal.state": "AP"}]}).pretty()
Output

Explanation: This query filters out documents where either the age
is 24 or the state
is "AP". The result will only include documents where neither condition is true.
Example 3: Matching Values in an Array using $nor
Operator
MongoDB's $nor
operator can also be used with arrays. In this example, we are looking for documents where the language
field does not contain either "Java" or "C++":
Query:
db.contributor.find({$nor: [{language: {$in: ["Java", "C++"]}}]}).pretty()
Output

Explanation: This query filters out documents where the language
field contains either "Java" or "C++". It returns documents where the language
is neither "Java" nor "C++".
When to Use the $nor
Operator in MongoDB?
The $nor
operator is highly useful when we need to:
- Exclude documents based on multiple conditions.
- Perform a logical NOT operation on combined conditions.
- Filter documents that do not meet a set of criteria.
Here are some scenarios where $nor
would be beneficial:
- Excluding specific categories or statuses from your query.
- Returning documents that don’t match certain flags or values in multiple fields.
- Complex exclusion logic in your MongoDB queries where you need more control over matching documents.
Advantages of Using $nor
in MongoDB
- Flexible Filtering:
$nor
provides a powerful way to filter documents based on complex, negative conditions, offering more flexibility than a simple negation. - Exclusion Logic: It is particularly useful when we want to exclude documents that meet any of the conditions in an array of expressions.
- Complex Queries: The
$nor
operator can be used with multiple other operators (like$eq
,$gt
,$lt
,$in
, etc.) to create complex exclusion logic.
Conclusion
The MongoDB $nor
operator is a versatile tool that allows us to create complex, negated query conditions in our MongoDB queries. By returning documents that do not match any of the specified conditions, it offers a powerful way to exclude data that doesn't meet specific criteria. Whether we're dealing with nested documents, arrays, or complex data structures, $nor
enables us to perform exclusion operations efficiently.