0
0
MongoDBquery~10 mins

Embedded documents (nested objects) in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Embedded documents (nested objects)
Start with main document
Add embedded document as a field
Store nested object inside main document
Query main document
Access nested fields via dot notation
Use or update nested data
End
This flow shows how a main document contains an embedded document as a nested object, which can be accessed or updated using dot notation.
Execution Sample
MongoDB
db.users.insertOne({
  name: "Alice",
  address: { city: "NY", zip: "10001" }
})
Insert a user document with an embedded address object containing city and zip.
Execution Table
StepActionDocument StateResult
1Prepare main document with name field{ name: "Alice" }Document ready for insertion
2Add embedded document 'address' with city and zip{ name: "Alice", address: { city: "NY", zip: "10001" } }Document now has nested object
3Insert document into 'users' collectionStored documentDocument saved with nested address
4Query document by nameFind document with name 'Alice'{ name: "Alice", address: { city: "NY", zip: "10001" } }
5Access nested field 'address.city'Access field"NY"
6Update nested field 'address.zip' to '10002'Modify nested fieldUpdated document with zip '10002'
7Query updated documentFind document{ name: "Alice", address: { city: "NY", zip: "10002" } }
💡 All steps complete; document inserted, queried, and updated with embedded document.
Variable Tracker
VariableStartAfter Step 2After Step 6Final
document{}{ name: "Alice", address: { city: "NY", zip: "10001" } }{ name: "Alice", address: { city: "NY", zip: "10002" } }{ name: "Alice", address: { city: "NY", zip: "10002" } }
Key Moments - 2 Insights
Why do we use dot notation to access nested fields?
Dot notation lets us reach inside the embedded document fields, like 'address.city', as shown in execution_table step 5.
Can we update just one field inside the embedded document without replacing the whole object?
Yes, as in step 6, we update 'address.zip' directly without changing 'address.city' or the whole document.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the value of 'address.city'?
A"NY"
B"10001"
C"Alice"
Dnull
💡 Hint
Check the 'Result' column at step 5 in execution_table.
At which step does the zip code change from '10001' to '10002'?
AStep 3
BStep 4
CStep 6
DStep 7
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for the update operation.
If we remove the embedded document 'address', what would the document look like after insertion?
A{ address: { city: "NY", zip: "10001" } }
B{ name: "Alice" }
C{}
D{ name: "Alice", address: null }
💡 Hint
Refer to the initial document state before adding the embedded document in execution_table step 1.
Concept Snapshot
Embedded documents store nested objects inside a main document.
Use dot notation to access or update nested fields.
Insert documents with embedded objects as normal fields.
Updates can target nested fields without replacing whole objects.
Queries can filter or project nested fields using dot notation.
Full Transcript
This visual execution trace shows how embedded documents work in MongoDB. We start with a main document containing a name field. Then we add an embedded document called address with city and zip fields. The document is inserted into the users collection. We query the document by name and access the nested city field using dot notation. We update the zip field inside the embedded address document without replacing the entire address. Finally, we query the updated document to see the changes. This demonstrates how nested objects are stored, accessed, and updated inside MongoDB documents.