0
0
MongoDBquery~10 mins

Arrays in documents in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Arrays in documents
Start with a document
Add an array field
Insert multiple values inside array
Query documents by array content
Update array elements
Use array operators for manipulation
Retrieve documents with arrays
End
This flow shows how a document can have an array field, how to add values, query, update, and retrieve arrays in MongoDB.
Execution Sample
MongoDB
db.products.insertOne({
  name: "Notebook",
  tags: ["stationery", "paper", "office"]
})
Insert a document with an array field 'tags' containing multiple string values.
Execution Table
StepActionDocument StateArray Field 'tags'Result
1Insert document with array{ name: "Notebook", tags: ["stationery", "paper", "office"] }["stationery", "paper", "office"]Document inserted
2Query documents where tags include 'paper'Same as step 1["stationery", "paper", "office"]Returns document with name 'Notebook'
3Update: add 'school' to tags array{ name: "Notebook", tags: ["stationery", "paper", "office", "school"] }["stationery", "paper", "office", "school"]Document updated
4Query documents where tags include 'school'Same as step 3["stationery", "paper", "office", "school"]Returns document with name 'Notebook'
5Remove 'office' from tags array{ name: "Notebook", tags: ["stationery", "paper", "school"] }["stationery", "paper", "school"]Document updated
6Query all documentsSame as step 5["stationery", "paper", "school"]Returns document with updated tags array
7ExitNo further changesNo changeEnd of operations
💡 All array operations completed and document state stable
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
Document{}{ name: "Notebook", tags: ["stationery", "paper", "office"] }{ name: "Notebook", tags: ["stationery", "paper", "office", "school"] }{ name: "Notebook", tags: ["stationery", "paper", "school"] }{ name: "Notebook", tags: ["stationery", "paper", "school"] }
tags arrayundefined["stationery", "paper", "office"]["stationery", "paper", "office", "school"]["stationery", "paper", "school"]["stationery", "paper", "school"]
Key Moments - 3 Insights
Why does querying with a value inside the array return the whole document?
Because MongoDB matches documents where the array contains the value, and returns the entire document, not just the array element (see execution_table step 2).
How does MongoDB add a new element to an existing array without replacing it?
Using update operators like $push, MongoDB appends the new element to the array, preserving existing elements (see execution_table step 3).
What happens if you remove an element from the array?
MongoDB updates the array by removing the specified element, and the document reflects this change (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the 'tags' array after step 3?
A["stationery", "paper"]
B["stationery", "paper", "office"]
C["stationery", "paper", "office", "school"]
D["school"]
💡 Hint
Check the 'Array Field tags' column in execution_table row for step 3
At which step is the element 'office' removed from the tags array?
AStep 5
BStep 2
CStep 3
DStep 6
💡 Hint
Look for the step where the tags array no longer contains 'office' in execution_table
If we query for documents with tag 'school' after step 1, what will happen?
AReturns the document
BReturns no documents
CReturns an error
DReturns only the tags array
💡 Hint
Check execution_table step 2 and step 4 to see when 'school' appears in tags
Concept Snapshot
Arrays in MongoDB documents store multiple values in one field.
Use square brackets [] to define arrays.
Query arrays by matching values inside them.
Update arrays with operators like $push and $pull.
The whole document is returned when querying by array content.
Full Transcript
This visual execution trace shows how arrays work inside MongoDB documents. We start by inserting a document with an array field called 'tags' containing three strings. Then we query documents that have a specific value inside the tags array, which returns the whole document. Next, we update the array by adding a new value 'school' using an update operation. We query again to confirm the new value is present. Then we remove an element 'office' from the array and query all documents to see the updated array. Finally, the operations end with the document reflecting all changes. This step-by-step trace helps beginners see how arrays are stored, queried, and updated inside MongoDB documents.