0
0
MongoDBquery~20 mins

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

Choose your learning style9 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.