Complete the code to add a new field 'total' that sums 'price' and 'tax' in each document.
db.sales.aggregate([{ $addFields: { total: { $[1]: ["$price", "$tax"] } } }])The $add expression adds numbers in MongoDB pipelines. Here, it sums 'price' and 'tax'.
Complete the code to filter documents where 'score' is greater than 80 using $match and $expr.
db.students.aggregate([{ $match: { $expr: { $[1]: ["$score", 80] } } }])The $gt expression checks if the first value is greater than the second.
Fix the error in the pipeline to correctly compute the average of 'marks' array using $avg.
db.exams.aggregate([{ $project: { average: { $[1]: "$marks" } } }])The $avg expression calculates the average of values in an array.
Fill both blanks to create a pipeline that filters documents where 'age' is less than 30 and then projects only 'name' and 'age'.
db.people.aggregate([{ $match: { $expr: { $[1]: ["$age", 30] } } }, { $project: { [2]: 1, age: 1 } }])$lt filters ages less than 30. Projecting 'name' and 'age' shows only these fields.
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.
db.products.aggregate([{ $addFields: { discountedPrice: { $[1]: ["$price", "$discount"] } } }, { $match: { $expr: { $[2]: ["$discountedPrice", [3]] } } }])$subtract calculates discounted price. $gt filters prices greater than 100.