0
0
MongoDBquery~10 mins

Array of embedded documents queries in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array of embedded documents queries
Start with collection
Identify array field
Use query operator on array
Match embedded document condition?
NoSkip document
Yes
Return matching documents
End
The query checks each document's array field for embedded documents matching the condition and returns those documents.
Execution Sample
MongoDB
db.orders.find({"items.product": "apple"})
Finds all orders where the items array contains an embedded document with product equal to 'apple'.
Execution Table
StepDocument _iditems ArrayCondition CheckMatch ResultAction
1order1[{product: 'apple', qty: 5}, {product: 'banana', qty: 2}]Check if any item.product == 'apple'TrueInclude document
2order2[{product: 'orange', qty: 3}, {product: 'banana', qty: 1}]Check if any item.product == 'apple'FalseSkip document
3order3[{product: 'apple', qty: 1}]Check if any item.product == 'apple'TrueInclude document
4order4[]Check if any item.product == 'apple'FalseSkip document
5order5[{product: 'grape', qty: 4}]Check if any item.product == 'apple'FalseSkip document
💡 All documents checked; only those with matching embedded documents are returned.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Current DocumentNoneorder1order2order3order4order5None
Match FoundFalseTrueFalseTrueFalseFalseN/A
Result Set Count0112222
Key Moments - 3 Insights
Why does the query match the whole document if only one embedded document in the array matches?
Because MongoDB returns the entire document if any embedded document in the array satisfies the condition, as shown in execution_table rows 1 and 3.
What happens if the array is empty or missing?
The condition check fails (False), so the document is skipped, as seen in execution_table row 4.
Can the query match multiple embedded documents in the same array?
Yes, but the query only requires at least one embedded document to match; it does not return multiple copies of the document.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Match Result for document 'order2'?
ATrue
BUnknown
CFalse
DError
💡 Hint
Check the 'Match Result' column for 'order2' in the execution_table.
At which step does the Result Set Count become 2?
AAfter step 2
BAfter step 3
CAfter step 4
DAfter step 5
💡 Hint
Look at the 'Result Set Count' row in variable_tracker after each step.
If the query condition changed to 'items.qty': {$gt: 3}, which document would match at step 1?
Aorder1
Border2
Corder3
Dorder4
💡 Hint
Check the 'items' array in execution_table row 1 and see if any qty is greater than 3.
Concept Snapshot
Querying arrays of embedded documents:
Use dot notation to specify fields inside embedded docs.
MongoDB matches documents if any array element meets the condition.
Empty or missing arrays do not match.
Returned documents include the whole document, not just matched elements.
Full Transcript
This visual execution shows how MongoDB queries work on arrays of embedded documents. Each document's array field is checked for embedded documents matching the query condition. If any embedded document matches, the whole document is included in the results. Documents with empty or missing arrays do not match. The variable tracker shows how the current document and match status change step by step, and the result set count increases only when a match is found. This helps beginners understand how array queries work in MongoDB.