0
0
MongoDBquery~10 mins

$push operator for adding to arrays 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 tag 'urgent' to the tags array in the document.

MongoDB
db.tasks.updateOne({ _id: 1 }, { [1]: { tags: 'urgent' } })
Drag options to blanks, or click blank then click option'
A$addToSet
B$push
C$set
D$inc
Attempts:
3 left
💡 Hint
Common Mistakes
Using $set replaces the whole array instead of adding an element.
Using $inc is for numbers, not arrays.
2fill in blank
medium

Complete the code to add the number 5 to the scores array in all documents where the name is 'Alice'.

MongoDB
db.students.updateMany({ name: 'Alice' }, { [1]: { scores: 5 } })
Drag options to blanks, or click blank then click option'
A$set
B$inc
C$push
D$pull
Attempts:
3 left
💡 Hint
Common Mistakes
Using $set replaces the whole array.
Using $pull removes elements, not adds.
3fill in blank
hard

Fix the error in the code to correctly add 'completed' to the status array for the document with _id 10.

MongoDB
db.orders.updateOne({ _id: 10 }, { [1]: { status: 'completed' } })
Drag options to blanks, or click blank then click option'
A$set
B$pull
C$addToSet
D$push
Attempts:
3 left
💡 Hint
Common Mistakes
Using $set replaces the entire status field.
Using $pull removes elements instead of adding.
4fill in blank
hard

Fill both blanks to add multiple tags 'new' and 'sale' to the tags array in one update.

MongoDB
db.products.updateOne({ _id: 5 }, { [1]: { tags: { [2]: ['new', 'sale'] } } })
Drag options to blanks, or click blank then click option'
A$push
B$each
C$addToSet
D$set
Attempts:
3 left
💡 Hint
Common Mistakes
Using $set replaces the whole array.
Not using $each adds the array as a single element.
5fill in blank
hard

Fill both blanks to add the numbers 10, 20, and 30 to the scores array only if they are not already present.

MongoDB
db.players.updateOne({ name: 'Bob' }, { [1]: { scores: { [2]: [10, 20, 30] } } })
Drag options to blanks, or click blank then click option'
A$addToSet
B$push
C$each
D$set
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push adds duplicates.
Not using $each adds the array as one element.