Challenge - 5 Problems
MongoDB $set Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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" } })Attempts:
2 left
💡 Hint
The $set operator changes existing fields or adds new fields without removing others.
✗ Incorrect
The $set operator updates the age from 25 to 26 and adds a new field city with value "New York". Other fields remain unchanged.
📝 Syntax
intermediate2: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?Attempts:
2 left
💡 Hint
The $set operator must be followed by an object with field-value pairs.
✗ Incorrect
Option C uses $set with an object correctly. Options A, B, and C have syntax errors or wrong usage.
❓ query_result
advanced2: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" } })Attempts:
2 left
💡 Hint
Using dot notation in $set updates nested fields inside the object.
✗ Incorrect
The $set with "profile.age" updates age to 31 inside profile. Adding "profile.city" adds city inside profile. So profile has name, age=31, city.
🔧 Debug
advanced2:00remaining
Why does this update fail?
You run this update:
But it throws an error. Why?
db.collection.updateOne({ _id: 3 }, { $set: { age } })But it throws an error. Why?
Attempts:
2 left
💡 Hint
Check the syntax inside the $set object.
✗ Incorrect
The $set operator expects an object with field names and their new values. Writing { age } alone is invalid syntax.
🧠 Conceptual
expert2:00remaining
What happens if you use $set with a field that is an array?
Given a document:
What will be the document after:
{ "_id": 4, "tags": ["red", "blue"] }What will be the document after:
db.collection.updateOne({ _id: 4 }, { $set: { tags: "green" } })Attempts:
2 left
💡 Hint
The $set operator replaces the entire field value with the new value.
✗ Incorrect
Using $set on an array field replaces the whole array with the new value. Here, tags becomes the string "green".