Bird
Raised Fist0
MongoDBquery~10 mins

$ne for not equal in MongoDB - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - $ne for not equal
Start Query
Check each document
Compare field value with $ne value
Include
Next Document
Return all included documents
End
The query checks each document's field. If the field value is not equal to the $ne value, the document is included in the result. Otherwise, it is excluded.
Execution Sample
MongoDB
db.products.find({ price: { $ne: 20 } })
Find all products where the price is not equal to 20.
Execution Table
StepDocumentField 'price' ValueCondition price != 20?Include in Result
1{ _id: 1, price: 15 }1515 != 20 is TrueYes
2{ _id: 2, price: 20 }2020 != 20 is FalseNo
3{ _id: 3, price: 25 }2525 != 20 is TrueYes
4{ _id: 4, price: 20 }2020 != 20 is FalseNo
5{ _id: 5, price: 30 }3030 != 20 is TrueYes
💡 All documents checked; only those with price not equal to 20 are included.
Variable Tracker
VariableStartAfter Doc 1After Doc 2After Doc 3After Doc 4After Doc 5Final
Included Documents[][{_id:1, price:15}][{_id:1, price:15}][{_id:1, price:15}, {_id:3, price:25}][{_id:1, price:15}, {_id:3, price:25}][{_id:1, price:15}, {_id:3, price:25}, {_id:5, price:30}][{_id:1, price:15}, {_id:3, price:25}, {_id:5, price:30}]
Key Moments - 2 Insights
Why are documents with price 20 excluded even though they exist in the collection?
Because $ne means 'not equal'. The condition price != 20 is false for these documents, so they are excluded as shown in execution_table rows 2 and 4.
Does $ne include documents where the field is missing?
Yes, documents missing the field are included by $ne. The condition is satisfied because the field does not exist and thus is not equal to 20. This example only shows documents with the field 'price'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which document is included at step 3?
A{ _id: 4, price: 20 }
B{ _id: 2, price: 20 }
C{ _id: 3, price: 25 }
D{ _id: 5, price: 30 }
💡 Hint
Check the 'Include in Result' column at step 3 in the execution_table.
At which step does the condition price != 20 become false for the first time?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look at the 'Condition price != 20?' column in execution_table rows.
If a document had price 20, would it be included in the final result?
AYes, always included
BNo, excluded because price equals 20
CYes, but only if another field matches
DNo, only if price is missing
💡 Hint
Refer to the meaning of $ne and the execution_table rows where price is 20.
Concept Snapshot
$ne operator in MongoDB
- Syntax: { field: { $ne: value } }
- Selects documents where field value is NOT equal to value
- Excludes documents where field equals value
- Includes documents missing the field
- Useful for filtering out specific values
Full Transcript
This visual execution shows how MongoDB's $ne operator works. The query checks each document's field value against the $ne value. If the field value is not equal, the document is included in the results. Documents with the field equal to the $ne value are excluded. The execution table traces each document's evaluation step-by-step. The variable tracker shows how the list of included documents grows. Key moments clarify common confusions about $ne behavior. The quiz tests understanding by referencing the execution visuals.

Practice

(1/5)
1.

What does the $ne operator do in MongoDB queries?

easy
A. Finds documents where a field is NOT equal to a specified value
B. Finds documents where a field is equal to a specified value
C. Finds documents where a field is greater than a specified value
D. Finds documents where a field is less than a specified value

Solution

  1. Step 1: Understand the purpose of $ne

    The $ne operator is used to filter documents where a field's value is not equal to the given value.
  2. Step 2: Compare with other operators

    Other operators like $eq check for equality, but $ne specifically excludes matching values.
  3. Final Answer:

    Finds documents where a field is NOT equal to a specified value -> Option A
  4. Quick Check:

    $ne = Not Equal [OK]
Hint: Remember: $ne means 'not equal' in queries [OK]
Common Mistakes:
  • Confusing $ne with $eq
  • Thinking $ne checks for greater or less than
  • Using $ne without a value
2.

Which of the following is the correct syntax to find documents where the field status is NOT equal to "active"?

{ status: { ? } }
easy
A. { $not: "active" }
B. { $eq: "active" }
C. { $ne: "active" }
D. { $neq: "active" }

Solution

  1. Step 1: Recall correct operator syntax

    The $ne operator is used with the syntax: { field: { $ne: value } } to find documents where the field is not equal to the value.
  2. Step 2: Check other options for correctness

    $eq checks equality, $not is used differently, and $neq is not a valid MongoDB operator.
  3. Final Answer:

    { $ne: "active" } -> Option C
  4. Quick Check:

    Correct syntax uses $ne [OK]
Hint: Use { field: { $ne: value } } for not equal queries [OK]
Common Mistakes:
  • Using $neq instead of $ne
  • Confusing $not with $ne
  • Missing curly braces around $ne
3.

Given the collection users with documents:

[{ "name": "Alice", "age": 25 }, { "name": "Bob", "age": 30 }, { "name": "Carol", "age": 25 }]

What will be the result of the query db.users.find({ age: { $ne: 25 } })?

medium
A. [{ "name": "Bob", "age": 30 }]
B. [{ "name": "Alice", "age": 25 }, { "name": "Carol", "age": 25 }]
C. [] (empty array)
D. All documents in the collection

Solution

  1. Step 1: Understand the query condition

    The query { age: { $ne: 25 } } finds documents where the age is NOT equal to 25.
  2. Step 2: Check each document's age

    Alice and Carol have age 25, so they are excluded. Bob has age 30, so he matches.
  3. Final Answer:

    [{ "name": "Bob", "age": 30 }] -> Option A
  4. Quick Check:

    Only age != 25 returns Bob [OK]
Hint: Exclude matching values with $ne to get others [OK]
Common Mistakes:
  • Including documents with age 25
  • Returning all documents by mistake
  • Confusing $ne with $eq
4.

Identify the error in this MongoDB query to find documents where category is NOT equal to "books":

db.collection.find({ category: { $ne: books } })
medium
A. The query is correct as is
B. Incorrect operator, should use $neq instead of $ne
C. The field name should be in quotes
D. Missing quotes around the string value "books"

Solution

  1. Step 1: Check the value type in the query

    The value "books" is a string and must be enclosed in quotes in MongoDB queries.
  2. Step 2: Verify operator and field name

    The operator $ne is correct, and field names do not require quotes unless special characters are present.
  3. Final Answer:

    Missing quotes around the string value "books" -> Option D
  4. Quick Check:

    String values need quotes [OK]
Hint: Always quote string values in queries [OK]
Common Mistakes:
  • Leaving string values unquoted
  • Using $neq instead of $ne
  • Quoting field names unnecessarily
5.

You have a collection products with documents containing type and price. You want to find all products that are NOT of type "electronics" and have a price NOT equal to 100. Which query correctly uses $ne to achieve this?

hard
A. { $ne: { type: "electronics", price: 100 } }
B. { type: { $ne: "electronics" }, price: { $ne: 100 } }
C. { type: { $ne: "electronics" } || price: { $ne: 100 } }
D. { type: { $not: "electronics" }, price: { $not: 100 } }

Solution

  1. Step 1: Use $ne on each field separately

    To find documents where type is not "electronics" and price is not 100, apply $ne to each field individually.
  2. Step 2: Check query syntax correctness

    { type: { $ne: "electronics" }, price: { $ne: 100 } } correctly uses { type: { $ne: "electronics" }, price: { $ne: 100 } }. { $ne: { type: "electronics", price: 100 } } misuses $ne on an object, { type: { $ne: "electronics" } || price: { $ne: 100 } } uses invalid syntax with ||, and { type: { $not: "electronics" }, price: { $not: 100 } } uses $not incorrectly.
  3. Final Answer:

    { type: { $ne: "electronics" }, price: { $ne: 100 } } -> Option B
  4. Quick Check:

    Apply $ne to each field separately [OK]
Hint: Use $ne on each field inside the query object [OK]
Common Mistakes:
  • Using $ne on an object instead of fields
  • Using logical OR (||) inside query object incorrectly
  • Confusing $not with $ne