0
0
MongoDBquery~20 mins

$set operator for setting fields in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $set Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of this update operation?
Consider a MongoDB collection users with a document:
{ "_id": 1, "name": "Alice", "age": 25 }
What will be the document after running this update?
db.users.updateOne({ _id: 1 }, { $set: { age: 26, city: "New York" } })
A{ "_id": 1, "name": "Alice", "age": 26, "city": "New York" }
B{ "_id": 1, "name": "Alice", "age": 25, "city": "New York" }
C{ "_id": 1, "name": "Alice", "age": 26 }
D{ "_id": 1, "name": "Alice", "city": "New York" }
Attempts:
2 left
💡 Hint
The $set operator changes existing fields or adds new fields without removing others.
📝 Syntax
intermediate
2:00remaining
Which update command uses $set correctly?
You want to update the field status to "active" in a MongoDB document. Which of these commands is syntactically correct?
Adb.collection.updateOne({ _id: 1 }, { $set: ["status", "active"] })
Bdb.collection.updateOne({ _id: 1 }, { set: { status: "active" } })
Cdb.collection.updateOne({ _id: 1 }, { $set: { status: "active" } })
Ddb.collection.updateOne({ _id: 1 }, { $set: status = "active" })
Attempts:
2 left
💡 Hint
The $set operator must be followed by an object with field-value pairs.
query_result
advanced
2:00remaining
What is the output after this update with nested fields?
Given a document:
{ "_id": 2, "profile": { "name": "Bob", "age": 30 } }
What will the document look like after:
db.collection.updateOne({ _id: 2 }, { $set: { "profile.age": 31, "profile.city": "Boston" } })
A{ "_id": 2, "profile": { "name": "Bob", "age": 30, "city": "Boston" } }
B{ "_id": 2, "profile": { "name": "Bob", "age": 31, "city": "Boston" } }
C{ "_id": 2, "profile": { "name": "Bob", "age": 31 }, "city": "Boston" }
D{ "_id": 2, "profile": { "name": "Bob", "age": 30 }, "profile.city": "Boston" }
Attempts:
2 left
💡 Hint
Using dot notation in $set updates nested fields inside the object.
🔧 Debug
advanced
2:00remaining
Why does this update fail?
You run this update:
db.collection.updateOne({ _id: 3 }, { $set: { age } })

But it throws an error. Why?
ABecause the collection name is missing.
BBecause the _id field cannot be used in update filters.
CBecause updateOne requires $inc operator instead of $set for numbers.
DBecause $set requires a field-value pair, but only a field name 'age' is given without a value.
Attempts:
2 left
💡 Hint
Check the syntax inside the $set object.
🧠 Conceptual
expert
2:00remaining
What happens if you use $set with a field that is an array?
Given a document:
{ "_id": 4, "tags": ["red", "blue"] }

What will be the document after:
db.collection.updateOne({ _id: 4 }, { $set: { tags: "green" } })
A{ "_id": 4, "tags": "green" }
B{ "_id": 4, "tags": ["red", "blue", "green"] }
C{ "_id": 4, "tags": ["green"] }
DThe update will fail with a type error.
Attempts:
2 left
💡 Hint
The $set operator replaces the entire field value with the new value.