0
0
MongoDBquery~10 mins

$all operator for matching all elements in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $all operator for matching all elements
Start Query
Check $all Operator
For each element in $all array
Does document array contain element?
Reject Document
All elements matched?
Include
Return matched documents
The $all operator checks if a document's array contains all specified elements. It tests each element and only returns documents matching all.
Execution Sample
MongoDB
db.products.find({ tags: { $all: ["red", "round"] } })
Finds products whose tags array contains both "red" and "round".
Execution Table
StepDocument tagsCheck element 'red'Check element 'round'Result
1["red", "round", "small"]YesYesInclude document
2["red", "square"]YesNoReject document
3["blue", "round"]NoYesReject document
4["red", "round"]YesYesInclude document
5["round"]NoYesReject document
Exit---All documents checked
💡 All documents checked; only those with both 'red' and 'round' included.
Variable Tracker
VariableStartDoc 1Doc 2Doc 3Doc 4Doc 5Final
tags-["red", "round", "small"]["red", "square"]["blue", "round"]["red", "round"]["round"]-
matches 'red'-truetruefalsetruefalse-
matches 'round'-truefalsetruetruetrue-
include document-truefalsefalsetruefalse-
Key Moments - 3 Insights
Why does a document with tags ["red", "square"] get rejected?
Because it does not contain the element 'round' required by $all, as shown in execution_table row 2 where 'Check element round' is No.
Does $all require the array elements to be in order?
No, $all only checks presence of all elements regardless of order, as seen in document 1 with tags ["red", "round", "small"] included despite order.
What happens if the document array is missing one element from $all?
The document is rejected, like document 5 missing 'red' and rejected in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, does the document with tags ["blue", "round"] match the query?
AYes, because it has 'round'
BNo, because it lacks 'red'
CYes, because it has at least one element
DNo, because it has 'blue'
💡 Hint
Check row 3 in the execution_table where 'matches red' is false and document is rejected.
At which step does the condition for including a document become false?
AWhen any element in $all is missing from the document array
BWhen the document array is empty
CWhen the document has extra elements
DWhen the document array has duplicates
💡 Hint
See execution_table rows 2, 3, and 5 where missing elements cause rejection.
If the query changes to $all: ["round"], which documents would be included?
ADocuments with 'red' only
BOnly documents with 'red' and 'round'
CDocuments with 'round' regardless of 'red'
DAll documents
💡 Hint
Focus on the $all array elements and their presence in document arrays.
Concept Snapshot
$all operator syntax:
{ field: { $all: [value1, value2, ...] } }
Returns documents where field array contains all listed values.
Order does not matter.
Useful to match multiple required elements in arrays.
Full Transcript
The $all operator in MongoDB is used to find documents where an array field contains all specified elements. The query checks each element in the $all array against the document's array field. If any element is missing, the document is excluded. Documents that contain all elements are included in the results. This operator does not require elements to be in order, only that all are present. For example, a query with $all: ["red", "round"] returns documents whose tags array includes both 'red' and 'round'. The execution table shows step-by-step checks for each document, confirming which elements are present and whether the document matches. This helps beginners understand how $all works by visualizing the matching process.