Comparison expressions help you check if values are equal or greater than others inside MongoDB aggregation. This lets you filter or create new data based on conditions.
Comparison expressions ($eq, $gt in aggregation) in MongoDB
{ $eq: [ <expression1>, <expression2> ] }
{ $gt: [ <expression1>, <expression2> ] }Both $eq and $gt take an array of two expressions to compare.
They return true or false depending on the comparison result.
status equals "A".{ $eq: [ "$status", "A" ] }score is greater than 80.{ $gt: [ "$score", 80 ] }age and minAge for equality.{ $eq: [ "$age", "$minAge" ] }This aggregation shows each student's name and two new fields: passed is true if their score is greater than 60, and perfectScore is true if their score equals 100.
db.students.aggregate([
{
$project: {
name: 1,
passed: { $gt: [ "$score", 60 ] },
perfectScore: { $eq: [ "$score", 100 ] }
}
}
])Remember to use the $ sign before field names inside expressions.
$eq and $gt return boolean values that you can use to filter or create new fields.
These expressions work inside aggregation pipelines, not in simple find queries.
$eq checks if two values are equal.
$gt checks if the first value is greater than the second.
Use them inside aggregation to compare fields or values and create conditions.
Practice
$eq operator do in a MongoDB aggregation pipeline?Solution
Step 1: Understand the purpose of
The$eq$eqoperator compares two values to see if they are exactly the same.Step 2: Compare with other operators
Unlike$gtwhich checks if one value is greater,$eqchecks equality.Final Answer:
Checks if two values are equal -> Option CQuick Check:
$eqmeans equal [OK]
- Confusing $eq with $gt
- Thinking $eq sorts data
- Using $eq to check field existence
$gt in a MongoDB aggregation $match stage to find documents where age is greater than 30?Solution
Step 1: Recall correct operator syntax
MongoDB operators like$gtare used as keys inside an object with the field name as the key.Step 2: Match correct structure
The correct syntax is{ field: { $gt: value } }, so{ age: { $gt: 30 } }is correct.Final Answer:
{ $match: { age: { $gt: 30 } } } -> Option BQuick Check:
Correct operator syntax = { $match: { age: { $gt: 30 } } } [OK]
- Placing $gt outside the field object
- Using parentheses like a function
- Missing $ before gt
{ $match: { score: { $gt: 80 } } }Which documents will be matched if the collection has documents with scores: 75, 80, 85, 90?
Solution
Step 1: Understand $gt meaning
The$gtoperator matches values strictly greater than the given number, here 80.Step 2: Check which scores are greater than 80
Scores 85 and 90 are greater than 80; 75 and 80 are not.Final Answer:
Documents with scores 85 and 90 -> Option DQuick Check:
Only scores > 80 match = Documents with scores 85 and 90 [OK]
- Including values equal to 80
- Confusing $gt with $gte
- Selecting lower values by mistake
$match stage:{ $match: { $eq: ["$status", "active"] } }Solution
Step 1: Check $eq usage in $match
The$eqoperator is usually used inside$expror aggregation expressions, not directly as a key in$match.Step 2: Understand correct placement
To compare fields with$eq, it must be inside$expr, like{ $match: { $expr: { $eq: ["$status", "active"] } } }.Final Answer:
Incorrect use of $eq outside a field key -> Option AQuick Check:
$eq needs $expr for expressions [OK]
- Using $eq directly in $match without $expr
- Confusing $eq array syntax
- Assuming $eq works like a filter key
price is greater than 100 and category equals "electronics" using aggregation. Which $match stage correctly uses $gt and $eq together?Solution
Step 1: Understand simple field comparisons
For simple equality, you can directly writecategory: "electronics"without using$eq.Step 2: Combine conditions in $match
MongoDB$matchsupports multiple field conditions combined with AND by default, so{ price: { $gt: 100 }, category: "electronics" }works.Step 3: Check other options
{ $match: { price: { $gt: 100 }, category: { $eq: "electronics" } } } misuses$eqinside$match(not needed). { $match: { $and: [ { price: { $gt: 100 } }, { category: { $eq: "electronics" } } ] } } uses$andunnecessarily. { $match: { $expr: { $and: [ { $gt: ["$price", 100] }, { $eq: ["$category", "electronics"] } ] } } } uses$exprwhich is more complex than needed.Final Answer:
{ $match: { price: { $gt: 100 }, category: "electronics" } } -> Option AQuick Check:
Simple conditions combine directly in $match [OK]
- Overusing $eq inside $match
- Using $and unnecessarily
- Using $expr when not needed
Ready to prove it?
Pass the test and this pattern moves unknown
