0
0
MongoDBquery~10 mins

$sum accumulator in MongoDB - Interactive Code Practice

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

Complete the code to sum the values of the "score" field in the aggregation.

MongoDB
db.scores.aggregate([{ $group: { _id: null, totalScore: { $sum: "$"[1] } } }])
Drag options to blanks, or click blank then click option'
Atotal
Bscore
Cvalue
Damount
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field name that does not exist in the documents.
Forgetting to include the $ sign before the field name.
2fill in blank
medium

Complete the code to sum the values of the "quantity" field grouped by "category".

MongoDB
db.products.aggregate([{ $group: { _id: "$"[1], totalQuantity: { $sum: "$quantity" } } }])
Drag options to blanks, or click blank then click option'
Acategory
Bprice
Cname
Dtype
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by a field that does not exist.
Using the wrong field name for grouping.
3fill in blank
hard

Fix the error in the aggregation pipeline to correctly sum the "amount" field.

MongoDB
db.transactions.aggregate([{ $group: { _id: null, totalAmount: { $sum: [1] } } }])
Drag options to blanks, or click blank then click option'
A$amount
B"amount"
Camount
D$"amount"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the field name in quotes inside $sum.
Forgetting the $ sign before the field name.
4fill in blank
hard

Fill both blanks to sum the "price" field grouped by "brand".

MongoDB
db.items.aggregate([{ $group: { _id: "$"[1], totalPrice: { $sum: "$"[2] } } }])
Drag options to blanks, or click blank then click option'
Abrand
Bcategory
Cprice
Dcost
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the grouping field and the sum field.
Using incorrect field names.
5fill in blank
hard

Fill all three blanks to sum the "score" field grouped by uppercase "player" names.

MongoDB
db.games.aggregate([{ $group: { _id: { [1]: { $toUpper: "$"[2] } }, totalScore: { $sum: "$"[3] } } }])
Drag options to blanks, or click blank then click option'
A$toUpper
Bplayer
Cscore
D$toLower
Attempts:
3 left
💡 Hint
Common Mistakes
Not using an expression for _id when transforming values.
Using wrong field names for sum or group.