0
0
MongoDBquery~10 mins

$addFields for computed fields 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 add a new field 'totalPrice' by multiplying 'price' and 'quantity'.

MongoDB
db.orders.aggregate([{ $addFields: { totalPrice: { $multiply: ["$price", [1]] } } }])
Drag options to blanks, or click blank then click option'
A"amount"
B"quantity"
C"$total"
D"$quantity"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the '$' prefix before field names.
Using the wrong field name without '$'.
2fill in blank
medium

Complete the code to add a field 'fullName' by concatenating 'firstName' and 'lastName' with a space.

MongoDB
db.users.aggregate([{ $addFields: { fullName: { $concat: ["$firstName", [1], "$lastName"] } } }])
Drag options to blanks, or click blank then click option'
A" "
B"-"
C","
D"_"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a dash or underscore instead of a space.
Forgetting to put the separator inside quotes.
3fill in blank
hard

Fix the error in the code to add a field 'discountedPrice' by subtracting 'discount' from 'price'.

MongoDB
db.sales.aggregate([{ $addFields: { discountedPrice: { $subtract: ["price", [1]] } } }])
Drag options to blanks, or click blank then click option'
A"$discount"
B"discount"
C"$price"
D"amount"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the '$' prefix before field names.
Using the wrong field name.
4fill in blank
hard

Fill both blanks to add a field 'averageScore' by dividing 'totalScore' by 'numTests'.

MongoDB
db.students.aggregate([{ $addFields: { averageScore: { $divide: ["[1]", "[2]"] } } }])
Drag options to blanks, or click blank then click option'
A$totalScore
B$numTests
CtotalScore
DnumTests
Attempts:
3 left
💡 Hint
Common Mistakes
Using field names without '$' prefix.
Swapping numerator and denominator.
5fill in blank
hard

Fill all three blanks to add a field 'summary' that concatenates uppercase 'category', a colon, and 'description'.

MongoDB
db.products.aggregate([{ $addFields: { summary: { $concat: [[1], [2], [3]] } } }])
Drag options to blanks, or click blank then click option'
A{'$toUpper': '$category'}
B": "
C$description
D$category
Attempts:
3 left
💡 Hint
Common Mistakes
Not using $toUpper for uppercase conversion.
Forgetting quotes around the colon and space string.
Using wrong field names.