0
0
MongoDBquery~10 mins

$set operator for setting fields in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $set operator for setting fields
Start with existing document
Apply $set operator
Check each field in $set
If field exists, update value
If field does not exist, add field with value
Save updated document
End
The $set operator updates specified fields in a document. It changes existing fields or adds new ones if they don't exist.
Execution Sample
MongoDB
db.collection.updateOne(
  { _id: 1 },
  { $set: { name: "Alice", age: 30 } }
)
This updates the document with _id 1, setting 'name' to 'Alice' and 'age' to 30.
Execution Table
StepDocument Before$set FieldsDocument AfterAction
1{ _id: 1, name: "Bob", city: "NY" }{ name: "Alice", age: 30 }{ _id: 1, name: "Alice", city: "NY", age: 30 }Updated 'name', added 'age'
2{ _id: 2, city: "LA" }{ name: "Alice", age: 30 }{ _id: 2, city: "LA", name: "Alice", age: 30 }Added 'name' and 'age'
3{ _id: 3, name: "Charlie", age: 25 }{ age: 30 }{ _id: 3, name: "Charlie", age: 30 }Updated 'age' only
4N/AN/AN/ANo more documents to update, operation ends
💡 All target documents processed, $set applied to each.
Variable Tracker
Document _idBefore $setAfter $set
1{ name: "Bob", city: "NY" }{ name: "Alice", city: "NY", age: 30 }
2{ city: "LA" }{ city: "LA", name: "Alice", age: 30 }
3{ name: "Charlie", age: 25 }{ name: "Charlie", age: 30 }
Key Moments - 2 Insights
Why does $set add new fields if they don't exist?
Because $set updates the document by changing existing fields or adding new ones if missing, as shown in execution_table rows 1 and 2.
What happens if $set updates a field that already exists?
The existing field's value is replaced with the new value, as seen in execution_table row 3 where 'age' changed from 25 to 30.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the 'age' value for document _id 1 after $set?
A25
Bnull
C30
DNot present
💡 Hint
Check the 'Document After' column for step 1 in execution_table.
At which step does $set add both 'name' and 'age' fields to a document that had neither?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Document Before' and 'Document After' columns in execution_table for step 2.
If we only $set { age: 35 } on document _id 3, what would be the 'age' after update?
A25
B35
C30
DField removed
💡 Hint
Refer to how $set updates existing fields in execution_table row 3.
Concept Snapshot
$set operator updates fields in a MongoDB document.
It replaces existing field values or adds new fields if missing.
Syntax: { $set: { field1: value1, field2: value2 } }
Used in update operations like updateOne or updateMany.
Does not remove fields, only sets or adds.
Full Transcript
The $set operator in MongoDB is used to update specific fields in documents. When you apply $set, it changes the value of existing fields or adds new fields if they don't exist. For example, if a document has a field 'name' with value 'Bob', using $set to set 'name' to 'Alice' will update it. If the document does not have an 'age' field, $set will add it with the specified value. This operation is commonly used in update commands like updateOne. The execution table shows step-by-step how documents change before and after applying $set. Key points include that $set never removes fields, only sets or adds them, and it works on each targeted document individually.