0
0
MongoDBquery~20 mins

$unset operator for removing fields in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Unsetter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output after using $unset to remove a field?
Consider a MongoDB collection named users with a document:
{ "_id": 1, "name": "Alice", "age": 30, "city": "New York" }

If you run the update:
db.users.updateOne({ _id: 1 }, { $unset: { age: "" } })

What will the document look like after this update?
A{ "_id": 1, "name": "Alice", "age": "", "city": "New York" }
B{ "_id": 1, "name": "Alice", "age": 30, "city": "New York" }
C{ "_id": 1, "name": "Alice", "age": null, "city": "New York" }
D{ "_id": 1, "name": "Alice", "city": "New York" }
Attempts:
2 left
💡 Hint
The $unset operator removes the specified field from the document.
🧠 Conceptual
intermediate
1:30remaining
What does the $unset operator do in MongoDB?
Choose the best description of the $unset operator in MongoDB.
AIt sets the value of a field to null.
BIt removes the specified field from a document.
CIt renames a field in a document.
DIt adds a new field with a specified value.
Attempts:
2 left
💡 Hint
Think about what happens to a field when you want it gone.
📝 Syntax
advanced
2:00remaining
Which update command correctly removes multiple fields using $unset?
You want to remove the fields email and phone from a document with { _id: 5 }. Which command is correct?
Adb.contacts.updateOne({ _id: 5 }, { $unset: { email: "", phone: "" } })
Bdb.contacts.updateOne({ _id: 5 }, { $unset: ["email", "phone"] })
Cdb.contacts.updateOne({ _id: 5 }, { $unset: { email: null, phone: null } })
Ddb.contacts.updateOne({ _id: 5 }, { $unset: { email: 0, phone: 0 } })
Attempts:
2 left
💡 Hint
The $unset operator expects an object with field names as keys.
🔧 Debug
advanced
2:00remaining
Why does this $unset update fail?
Given the command:
db.products.updateOne({ _id: 10 }, { $unset: { 'details.price' } })

The update fails with a syntax error. What is the cause?
AThe $unset value for each field must be specified, even if empty.
BupdateOne requires a third argument for $unset to work.
CField names cannot contain dots in $unset.
DThe filter {_id: 10} is invalid for $unset.
Attempts:
2 left
💡 Hint
Check the syntax of the $unset object carefully.
optimization
expert
3:00remaining
Efficiently removing multiple fields from many documents
You have a large collection orders with millions of documents. You want to remove the fields tempData and oldStatus from all documents. Which approach is best for performance?
ADrop the collection and recreate it without those fields.
BRun multiple updateOne commands for each document to remove one field at a time.
CRun a single updateMany with { $unset: { tempData: "", oldStatus: "" } } without a filter.
DUse find() to get all documents, then update each document individually in the application.
Attempts:
2 left
💡 Hint
Think about minimizing the number of operations sent to the database.