A. Missing $set operator, so it replaces the whole document.
B. The filter query is incorrect syntax.
C. updateOne cannot update numeric fields.
D. The collection name is invalid.
Solution
Step 1: Check update command structure
The update document must use an operator like $set to update fields without replacing the whole document.
Step 2: Understand effect of missing $set
Without $set, the document is replaced entirely with {score: 100}, losing other fields.
Final Answer:
Missing $set operator, so it replaces the whole document. -> Option A
Quick Check:
Always use $set to update fields [OK]
Hint: Always include $set to update fields safely [OK]
Common Mistakes:
Omitting $set and replacing document
Thinking updateOne syntax is wrong
Believing updateOne can't update numbers
5. You want to update the status field to "active" only if it currently exists in the document. Which update command achieves this safely without creating new fields?
hard
A. db.collection.updateMany({status: {$exists: false}}, {$set: {status: "active"}})
B. db.collection.updateMany({}, {$set: {status: "active"}})
C. db.collection.updateMany({status: null}, {$set: {status: "active"}})
D. db.collection.updateMany({status: {$exists: true}}, {$set: {status: "active"}})
Solution
Step 1: Use filter to check field existence
The filter {status: {$exists: true}} selects documents where status field exists.
Step 2: Update only matching documents
The $set updates status to "active" only for those documents, avoiding creating new fields.
Final Answer:
db.collection.updateMany({status: {$exists: true}}, {$set: {status: "active"}}) -> Option D
Quick Check:
Filter with $exists true to update safely = A [OK]
Hint: Filter with $exists:true to update only existing fields [OK]
Common Mistakes:
Updating all documents regardless of field existence