0
0
MongoDBquery~10 mins

Why expressions matter in pipelines in MongoDB - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a new field 'total' that sums 'price' and 'tax' in each document.

MongoDB
db.sales.aggregate([{ $addFields: { total: { $[1]: ["$price", "$tax"] } } }])
Drag options to blanks, or click blank then click option'
Aadd
Bsum
Cmultiply
Dconcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $add inside $addFields
Using $multiply which multiplies values
Using $concat which joins strings
2fill in blank
medium

Complete the code to filter documents where 'score' is greater than 80 using $match and $expr.

MongoDB
db.students.aggregate([{ $match: { $expr: { $[1]: ["$score", 80] } } }])
Drag options to blanks, or click blank then click option'
Aeq
Bgt
Clt
Dne
Attempts:
3 left
💡 Hint
Common Mistakes
Using $eq which checks equality
Using $lt which checks less than
Using $ne which checks not equal
3fill in blank
hard

Fix the error in the pipeline to correctly compute the average of 'marks' array using $avg.

MongoDB
db.exams.aggregate([{ $project: { average: { $[1]: "$marks" } } }])
Drag options to blanks, or click blank then click option'
Asum
Bmin
Cmax
Davg
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum which adds values
Using $max or $min which find extremes
4fill in blank
hard

Fill both blanks to create a pipeline that filters documents where 'age' is less than 30 and then projects only 'name' and 'age'.

MongoDB
db.people.aggregate([{ $match: { $expr: { $[1]: ["$age", 30] } } }, { $project: { [2]: 1, age: 1 } }])
Drag options to blanks, or click blank then click option'
Alt
Bgt
Cname
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using $gt instead of $lt
Projecting wrong fields like 'score'
5fill in blank
hard

Fill all three blanks to create a pipeline that adds a field 'discountedPrice' by subtracting 'discount' from 'price', then filters where 'discountedPrice' is greater than 100.

MongoDB
db.products.aggregate([{ $addFields: { discountedPrice: { $[1]: ["$price", "$discount"] } } }, { $match: { $expr: { $[2]: ["$discountedPrice", [3]] } } }])
Drag options to blanks, or click blank then click option'
Asubtract
Bgt
C100
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using $add instead of $subtract
Using $lt instead of $gt
Filtering with wrong value