0
0
MongoDBquery~10 mins

$each modifier with $push 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 multiple items to the array field using $push with $each.

MongoDB
db.collection.updateOne({ _id: 1 }, { $push: { scores: { $each: [1] } } })
Drag options to blanks, or click blank then click option'
A85, 90, 95
B{85, 90, 95}
C(85, 90, 95)
D[85, 90, 95]
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces instead of square brackets for the list of values.
Passing values without an array.
2fill in blank
medium

Complete the code to add multiple tags to a document's tags array using $push and $each.

MongoDB
db.collection.updateOne({ name: 'book' }, { $push: { tags: { $each: [1] } } })
Drag options to blanks, or click blank then click option'
A['fiction', 'bestseller']
B{'fiction', 'bestseller'}
C'fiction', 'bestseller'
D(fiction, bestseller)
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes without brackets, which is not an array.
Using parentheses or curly braces instead of square brackets.
3fill in blank
hard

Fix the error in the update command to correctly add multiple scores using $push with $each.

MongoDB
db.collection.updateOne({ _id: 2 }, { $push: { scores: { $each: [1] } } })
Drag options to blanks, or click blank then click option'
A{88, 92, 85}
B[88, 92, 85]
C88, 92, 85
D(88, 92, 85)
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces or parentheses instead of square brackets.
Passing values without any brackets.
4fill in blank
hard

Fill both blanks to add multiple new tags and sort them in ascending order.

MongoDB
db.collection.updateOne({ _id: 3 }, { $push: { tags: { $each: [1], $sort: [2] } } })
Drag options to blanks, or click blank then click option'
A['new', 'sale']
B1
C-1
D['old', 'clearance']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-array value for $each.
Using -1 for ascending sort instead of 1.
5fill in blank
hard

Fill all three blanks to add multiple scores, slice the array to keep only the last 3, and sort descending.

MongoDB
db.collection.updateOne({ _id: 4 }, { $push: { scores: { $each: [1], $slice: [2], $sort: [3] } } })
Drag options to blanks, or click blank then click option'
A[70, 75, 80]
B-3
C-1
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using positive number for $slice to keep first elements instead of last.
Using 1 for $sort when descending order is needed.