0
0
MongoDBquery~10 mins

Querying array elements directly in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Querying array elements directly
Start with collection
Specify query on array field
MongoDB checks each array element
Match elements that satisfy condition?
Yes No
Return document
End
MongoDB checks each element in the array field to find if any element matches the query condition, then returns the whole document if a match is found.
Execution Sample
MongoDB
db.students.find({ scores: { $elemMatch: { score: { $gt: 80 } } } })
Finds all student documents where at least one score in the scores array is greater than 80.
Execution Table
StepDocumentArray Field 'scores'Condition CheckMatch ResultAction
1{name: 'Alice', scores: [{score: 75}, {score: 85}]}[75, 85]Check 75 > 80? NoNo match yetContinue checking
2{name: 'Alice', scores: [{score: 75}, {score: 85}]}[75, 85]Check 85 > 80? YesMatch foundReturn document
3{name: 'Bob', scores: [{score: 60}, {score: 70}]}[60, 70]Check 60 > 80? NoNo match yetContinue checking
4{name: 'Bob', scores: [{score: 60}, {score: 70}]}[60, 70]Check 70 > 80? NoNo matchSkip document
5{name: 'Carol', scores: [{score: 90}]}[90]Check 90 > 80? YesMatch foundReturn document
6End of collection---Query complete
💡 All documents checked; query returns documents where any array element matches condition.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Current DocumentNoneAliceAliceBobBobCarolEnd
Array Elements CheckedNone7585607090Complete
Match FoundFalseFalseTrueFalseFalseTrueFinal
Key Moments - 2 Insights
Why does MongoDB return the whole document even if only one array element matches?
Because the query checks if any element in the array matches the condition, and if yes, the entire document containing that array is returned, as shown in execution_table rows 2 and 5.
What happens if no array elements satisfy the condition?
MongoDB skips that document entirely, as seen in execution_table rows 4 where no scores are greater than 80, so the document is not returned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the match result when checking Alice's score 75?
ANo match yet
BMatch found
CNo match
DQuery complete
💡 Hint
Refer to execution_table row 1 under 'Match Result' column.
At which step does the query decide to skip Bob's document?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Check execution_table row 4 for the action taken on Bob's document.
If the condition changed to score > 90, which document would still be returned?
AAlice
BNone
CCarol
DBob
💡 Hint
Look at the scores in variable_tracker and see which scores are greater than 90.
Concept Snapshot
Querying array elements directly in MongoDB:
Use $elemMatch to specify conditions on array elements.
MongoDB checks each element; if any matches, returns the whole document.
If no elements match, document is skipped.
Example: db.collection.find({arrayField: {$elemMatch: {condition}}})
Full Transcript
This visual execution shows how MongoDB queries documents by checking each element inside an array field. For each document, MongoDB looks at every element in the array to see if it meets the query condition. If any element matches, the entire document is returned. If none match, the document is skipped. For example, when querying students with scores greater than 80, MongoDB checks each score in the scores array. Alice has a score 85 which matches, so her document is returned. Bob's scores do not match, so his document is skipped. Carol has a score 90 which matches, so her document is returned. This process continues until all documents are checked. This helps beginners understand that MongoDB does not return just the matching array element but the whole document containing it.