Bird
Raised Fist0
MongoDBquery~10 mins

Comparison expressions ($eq, $gt in aggregation) in MongoDB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Comparison expressions ($eq, $gt in aggregation)
Start aggregation pipeline
↓
Apply $match stage
↓
Evaluate $eq or $gt expression
↓
Filter documents based on comparison
↓
Output filtered documents
↓
End
The aggregation pipeline starts, applies a $match stage where $eq or $gt expressions compare fields to values, filters documents accordingly, and outputs the result.
Execution Sample
MongoDB
db.products.aggregate([
  { $match: { price: { $gt: 100 } } }
])
Filters products with price greater than 100 using $gt in aggregation.
Execution Table
StepDocumentCondition EvaluatedResultAction
1{_id:1, price:50}price > 100FalseExclude document
2{_id:2, price:150}price > 100TrueInclude document
3{_id:3, price:100}price > 100FalseExclude document
4{_id:4, price:200}price > 100TrueInclude document
End---No more documents to evaluate
💡 All documents processed; only those with price > 100 included.
Variable Tracker
VariableStartAfter Doc 1After Doc 2After Doc 3After Doc 4Final
Included Documents[][][{_id:2, price:150}][{_id:2, price:150}][{_id:2, price:150}, {_id:4, price:200}][{_id:2, price:150}, {_id:4, price:200}]
Key Moments - 2 Insights
Why is the document with price 100 excluded when using $gt: 100?
Because $gt means 'greater than', not 'greater than or equal to'. The execution_table row 3 shows price 100 evaluated as False.
How does $eq differ from $gt in filtering documents?
$eq checks for exact equality, while $gt checks if a value is strictly greater. The execution_table shows $gt filtering; $eq would include only documents matching exactly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which document is included at step 2?
A{_id:2, price:150}
B{_id:1, price:50}
C{_id:3, price:100}
D{_id:4, price:200}
💡 Hint
Check the 'Result' and 'Action' columns at step 2 in execution_table.
At which step does the condition 'price > 100' become false for the first time?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Result' column in execution_table for step 1.
If we change $gt: 100 to $eq: 100, which document would be included?
A{_id:2, price:150}
B{_id:1, price:50}
C{_id:3, price:100}
D{_id:4, price:200}
💡 Hint
Refer to the document with price exactly 100 in execution_table.
Concept Snapshot
Aggregation $match uses comparison expressions like $eq and $gt.
$eq checks if a field equals a value.
$gt checks if a field is greater than a value.
Documents passing the condition are included in output.
Useful for filtering data in aggregation pipelines.
Full Transcript
This visual execution shows how MongoDB aggregation uses comparison expressions $eq and $gt inside a $match stage. Each document is checked against the condition, for example price > 100. Documents with price 150 and 200 pass and are included, while those with 50 and 100 do not. The variable tracker shows how the included documents list grows. Key moments clarify why 100 is excluded with $gt and how $eq differs. The quiz tests understanding of which documents pass and how changing the operator affects results.

Practice

(1/5)
1. What does the $eq operator do in a MongoDB aggregation pipeline?
easy
A. Checks if one value is greater than another
B. Sorts documents in ascending order
C. Checks if two values are equal
D. Filters documents by existence of a field

Solution

  1. Step 1: Understand the purpose of $eq

    The $eq operator compares two values to see if they are exactly the same.
  2. Step 2: Compare with other operators

    Unlike $gt which checks if one value is greater, $eq checks equality.
  3. Final Answer:

    Checks if two values are equal -> Option C
  4. Quick Check:

    $eq means equal [OK]
Hint: Remember: eq means equal, gt means greater [OK]
Common Mistakes:
  • Confusing $eq with $gt
  • Thinking $eq sorts data
  • Using $eq to check field existence
2. Which of the following is the correct syntax to use $gt in a MongoDB aggregation $match stage to find documents where age is greater than 30?
easy
A. { $match: { $gt: { age: 30 } } }
B. { $match: { age: { $gt: 30 } } }
C. { $match: { age: $gt(30) } }
D. { $match: { age: { gt: 30 } } }

Solution

  1. Step 1: Recall correct operator syntax

    MongoDB operators like $gt are used as keys inside an object with the field name as the key.
  2. Step 2: Match correct structure

    The correct syntax is { field: { $gt: value } }, so { age: { $gt: 30 } } is correct.
  3. Final Answer:

    { $match: { age: { $gt: 30 } } } -> Option B
  4. Quick Check:

    Correct operator syntax = { $match: { age: { $gt: 30 } } } [OK]
Hint: Operators go inside field object with $ prefix [OK]
Common Mistakes:
  • Placing $gt outside the field object
  • Using parentheses like a function
  • Missing $ before gt
3. Given the aggregation pipeline stage:
{ $match: { score: { $gt: 80 } } }

Which documents will be matched if the collection has documents with scores: 75, 80, 85, 90?
medium
A. Documents with scores 75, 85, and 90
B. Documents with scores 75 and 80
C. Documents with scores 80, 85, and 90
D. Documents with scores 85 and 90

Solution

  1. Step 1: Understand $gt meaning

    The $gt operator matches values strictly greater than the given number, here 80.
  2. Step 2: Check which scores are greater than 80

    Scores 85 and 90 are greater than 80; 75 and 80 are not.
  3. Final Answer:

    Documents with scores 85 and 90 -> Option D
  4. Quick Check:

    Only scores > 80 match = Documents with scores 85 and 90 [OK]
Hint: Remember $gt excludes equal values [OK]
Common Mistakes:
  • Including values equal to 80
  • Confusing $gt with $gte
  • Selecting lower values by mistake
4. Identify the error in this aggregation $match stage:
{ $match: { $eq: ["$status", "active"] } }
medium
A. Incorrect use of $eq outside a field key
B. Missing $gt operator
C. Wrong array syntax for $eq
D. No error, syntax is correct

Solution

  1. Step 1: Check $eq usage in $match

    The $eq operator is usually used inside $expr or aggregation expressions, not directly as a key in $match.
  2. Step 2: Understand correct placement

    To compare fields with $eq, it must be inside $expr, like { $match: { $expr: { $eq: ["$status", "active"] } } }.
  3. Final Answer:

    Incorrect use of $eq outside a field key -> Option A
  4. Quick Check:

    $eq needs $expr for expressions [OK]
Hint: Use $expr to compare fields with $eq [OK]
Common Mistakes:
  • Using $eq directly in $match without $expr
  • Confusing $eq array syntax
  • Assuming $eq works like a filter key
5. You want to filter documents where price is greater than 100 and category equals "electronics" using aggregation. Which $match stage correctly uses $gt and $eq together?
hard
A. { $match: { price: { $gt: 100 }, category: "electronics" } }
B. { $match: { $and: [ { price: { $gt: 100 } }, { category: { eq: "electronics" } } ] } }
C. { $match: { price: { $gt: 100 }, category: { eq: "electronics" } } }
D. { $match: { $expr: { $and: [ { gt: ["$price", 100] }, { eq: ["$category", "electronics"] } ] } } }

Solution

  1. Step 1: Understand simple field comparisons

    For simple equality, you can directly write category: "electronics" without using $eq.
  2. Step 2: Combine conditions in $match

    MongoDB $match supports multiple field conditions combined with AND by default, so { price: { $gt: 100 }, category: "electronics" } works.
  3. Step 3: Check other options

    { $match: { price: { $gt: 100 }, category: { $eq: "electronics" } } } misuses $eq inside $match (not needed). { $match: { $and: [ { price: { $gt: 100 } }, { category: { $eq: "electronics" } } ] } } uses $and unnecessarily. { $match: { $expr: { $and: [ { $gt: ["$price", 100] }, { $eq: ["$category", "electronics"] } ] } } } uses $expr which is more complex than needed.
  4. Final Answer:

    { $match: { price: { $gt: 100 }, category: "electronics" } } -> Option A
  5. Quick Check:

    Simple conditions combine directly in $match [OK]
Hint: Use direct equality for fields, $gt for ranges [OK]
Common Mistakes:
  • Overusing $eq inside $match
  • Using $and unnecessarily
  • Using $expr when not needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard