0
0
MongoDBquery~10 mins

Single document atomicity 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 update a single field atomically in a MongoDB document.

MongoDB
db.collection.updateOne({ _id: 1 }, { [1]: { score: 100 } })
Drag options to blanks, or click blank then click option'
A$push
B$inc
C$set
D$unset
Attempts:
3 left
💡 Hint
Common Mistakes
Using $inc when you want to set a specific value.
Using $push which is for arrays, not single fields.
2fill in blank
medium

Complete the code to increment a numeric field atomically in a MongoDB document.

MongoDB
db.collection.updateOne({ _id: 2 }, { [1]: { visits: 1 } })
Drag options to blanks, or click blank then click option'
A$set
B$addToSet
C$push
D$inc
Attempts:
3 left
💡 Hint
Common Mistakes
Using $set which replaces the value instead of incrementing.
Using $push which is for arrays.
3fill in blank
hard

Fix the error in the update command to atomically add a value to an array field without duplicates.

MongoDB
db.collection.updateOne({ _id: 3 }, { [1]: { tags: "mongodb" } })
Drag options to blanks, or click blank then click option'
A$push
B$addToSet
C$set
D$inc
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push which can add duplicates.
Using $set which replaces the whole array.
4fill in blank
hard

Fill both blanks to atomically update multiple fields in a single document.

MongoDB
db.collection.updateOne({ _id: 4 }, { [1]: { score: 90, level: 5 } })
Drag options to blanks, or click blank then click option'
A$set
B$inc
C$unset
D$push
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing $set with $inc or other operators incorrectly.
Using $unset which removes fields instead of updating.
5fill in blank
hard

Fill all three blanks to atomically update a document: increment visits, add a tag uniquely, and set status.

MongoDB
db.collection.updateOne({ _id: 5 }, { [1]: { visits: 1 }, [2]: { tags: "new" }, [3]: { status: "active" } })
Drag options to blanks, or click blank then click option'
A$inc
B$addToSet
C$set
D$push
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push instead of $addToSet causing duplicates.
Using $set to increment numbers which replaces values.