0
0
MongoDBquery~10 mins

$slice 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 a value to the 'scores' array using $push.

MongoDB
db.students.updateOne({name: 'Alice'}, {$push: {scores: [1])
Drag options to blanks, or click blank then click option'
A95
B[95]
C{95}
D"95"
Attempts:
3 left
💡 Hint
Common Mistakes
Using brackets [] which adds an array instead of a single value.
2fill in blank
medium

Complete the code to limit the 'scores' array to the last 3 elements after pushing a new score.

MongoDB
db.students.updateOne({name: 'Bob'}, {$push: {scores: {$each: [88], [1]: -3}}})
Drag options to blanks, or click blank then click option'
A$count
B$limit
C$max
D$slice
Attempts:
3 left
💡 Hint
Common Mistakes
Using $limit which is not valid in this context.
3fill in blank
hard

Fix the error in the code to correctly push multiple scores and keep only the last 5.

MongoDB
db.students.updateOne({name: 'Carol'}, {$push: {scores: {$each: [1], $slice: -5}}})
Drag options to blanks, or click blank then click option'
A"70, 75, 80"
B{70, 75, 80}
C[70, 75, 80]
D70, 75, 80
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} which is for objects, not arrays.
4fill in blank
hard

Fill both blanks to push scores and keep only the first 4 elements in the array.

MongoDB
db.students.updateOne({name: 'Dave'}, {$push: {scores: {$each: [1], [2]: 4}}})
Drag options to blanks, or click blank then click option'
A[85, 90]
B$slice
C$limit
D$count
Attempts:
3 left
💡 Hint
Common Mistakes
Using $limit which is not a valid modifier here.
5fill in blank
hard

Fill all three blanks to push a score, keep last 3 elements, and sort scores descending.

MongoDB
db.students.updateOne({name: 'Eve'}, {$push: {scores: {$each: [[1]], [2]: -3, [3]: -1}}})
Drag options to blanks, or click blank then click option'
A92
B$slice
C$sort
D$limit
Attempts:
3 left
💡 Hint
Common Mistakes
Using $limit which is not a valid modifier here.