Bird
Raised Fist0
MongoDBquery~20 mins

Comparison expressions ($eq, $gt in aggregation) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MongoDB Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Find documents where age equals 30
Given a collection users with documents containing an age field, which aggregation pipeline stage will return only documents where age is exactly 30?
MongoDB
db.users.aggregate([
  { $match: { $expr: { $eq: ["$age", 30] } } }
])
Adb.users.aggregate([{ $match: { $expr: { $gt: ["$age", 30] } } }])
Bdb.users.aggregate([{ $match: { age: { $gt: 30 } } }])
Cdb.users.aggregate([{ $match: { $expr: { $eq: ["$age", 30] } } }])
Ddb.users.aggregate([{ $match: { age: 30 } }])
Attempts:
2 left
💡 Hint
Use $expr with $eq to compare fields inside aggregation.
❓ query_result
intermediate
2:00remaining
Filter documents with score greater than 80
Which aggregation pipeline stage filters documents where the score field is greater than 80?
MongoDB
db.scores.aggregate([
  { $match: { $expr: { $gt: ["$score", 80] } } }
])
Adb.scores.aggregate([{ $match: { score: { $gt: 80 } } }])
Bdb.scores.aggregate([{ $match: { score: 80 } }])
Cdb.scores.aggregate([{ $match: { $expr: { $eq: ["$score", 80] } } }])
Ddb.scores.aggregate([{ $match: { $expr: { $gt: ["$score", 80] } } }])
Attempts:
2 left
💡 Hint
Use $expr with $gt to compare field values inside aggregation.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this aggregation filter
Which option contains a syntax error in the aggregation pipeline that filters documents where price is greater than 100?
MongoDB
db.products.aggregate([
  { $match: { $expr: { $gt: ["$price", 100] } } }
])
A)]} } } ]001 ,"ecirp$"[ :tg$ { :rpxe$ { :hctam$ {[(etagergga.stcudorp.bd
Bdb.products.aggregate([{ $match: { $expr: { $gt: ["$price", 100] } } }])
Cdb.products.aggregate([{ $match: { price: { $gt: 100 } } }])
Db.products.aggregate([{ $match: { $expr: { $gt: ["$price", 100] } } }])
Attempts:
2 left
💡 Hint
Check the syntax of $gt operator arguments inside $expr.
❓ optimization
advanced
2:00remaining
Optimize filtering for equality in aggregation
Which option is the most efficient way to filter documents where status equals "active" in an aggregation pipeline?
Adb.collection.aggregate([{ $match: { status: "active" } }])
Bdb.collection.aggregate([{ $match: { $expr: { $eq: ["$status", "active"] } } }])
Cdb.collection.aggregate([{ $match: { $expr: { $gt: ["$status", "active"] } } }])
Ddb.collection.aggregate([{ $match: { status: { $eq: "active" } } }])
Attempts:
2 left
💡 Hint
Simple equality filters can be done directly without $expr for better performance.
🧠 Conceptual
expert
3:00remaining
Understanding $expr with $eq and $gt in aggregation
Consider the aggregation pipeline stage: { $match: { $expr: { $and: [ { $eq: ["$type", "A"] }, { $gt: ["$value", 10] } ] } } }. What does this stage do?
AFilters documents where type is "A" and value is greater than 10
BFilters documents where type is "A" or value is greater than 10
CFilters documents where type is not "A" and value is less than or equal to 10
DFilters documents where type is "A" and value is less than 10
Attempts:
2 left
💡 Hint
Look at the logical operator $and combining $eq and $gt conditions.

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