What if your database could instantly find and count exactly what you need inside messy lists?
Why Array expressions ($size, $arrayElemAt, $filter) in MongoDB? - Purpose & Use Cases
Imagine you have a big box of mixed toys, and you want to count how many cars are inside, pick the third toy, or find only the red toys. Doing this by hand means opening the box, sorting through every toy one by one, and counting or picking manually.
Manually counting or picking toys is slow and tiring. You might lose track, make mistakes, or take a long time to find what you want. If the box is huge, it becomes almost impossible to do quickly and correctly.
Array expressions like $size, $arrayElemAt, and $filter let you ask MongoDB to do this work for you. They quickly count items, pick specific elements, or filter arrays based on conditions, all inside the database without extra manual effort.
count = 0 for toy in box: if toy.type == 'car': count += 1 third_toy = box[2] red_toys = [toy for toy in box if toy.color == 'red']
[{ $size: "$toys" }, { $arrayElemAt: ["$toys", 2] }, { $filter: { input: "$toys", as: "toy", cond: { $eq: ["$$toy.color", "red"] } } }]These expressions let you quickly analyze and manipulate lists inside your data, making your queries smarter and faster.
For an online store, you can count how many items a customer has in their cart, pick the first item they added, or find all items on sale, all with simple array expressions.
Manual counting and filtering is slow and error-prone.
Array expressions automate counting, picking, and filtering inside the database.
This makes data queries faster, simpler, and more reliable.
Practice
$size do when used on an array field?Solution
Step 1: Understand the purpose of
The$size$sizeoperator counts the number of elements inside an array.Step 2: Compare with other operators
$arrayElemAtpicks an element by position,$filterselects elements by condition, and sorting is unrelated.Final Answer:
Counts how many elements are in the array -> Option AQuick Check:
$size= count elements [OK]
- Confusing $size with $arrayElemAt
- Thinking $size filters elements
- Assuming $size sorts arrays
items using MongoDB aggregation?Solution
Step 1: Recall zero-based indexing in $arrayElemAt
The third element is at index 2 because counting starts at 0.Step 2: Check syntax correctness
The correct syntax uses$arrayElemAtwith the array field prefixed by$and the zero-based index.Final Answer:
{ $arrayElemAt: ["$items", 2] } -> Option CQuick Check:
Third element index = 2 [OK]
- Using 1-based index instead of 0-based
- Missing $ before field name
- Confusing $size with $arrayElemAt
{ scores: [10, 20, 30, 40] }, what will the aggregation expression { $size: "$scores" } return?Solution
Step 1: Understand what
$sizedoes$sizecounts the number of elements in the array.Step 2: Count elements in the array
The array[10, 20, 30, 40]has 4 elements.Final Answer:
4 -> Option AQuick Check:
Array length = 4 [OK]
- Returning the third element instead of count
- Returning the whole array instead of count
- Assuming syntax error
{ $arrayElemAt: ["$values", "1"] }Solution
Step 1: Check the type of index in $arrayElemAt
The index must be a number, but here it is a string "1".Step 2: Confirm correct field referencing
The field name "$values" is correctly prefixed with$.Final Answer:
Index must be a number, not a string -> Option BQuick Check:
Index type = number, not string [OK]
- Using string instead of number for index
- Omitting $ before field name
- Thinking array must be wrapped differently
tags as an array of strings. How would you write a MongoDB aggregation expression to get the count of tags that start with the letter 'a'?Solution
Step 1: Use $filter to select tags starting with 'a'
The$filteroperator creates a new array with tags matching the regex /^a/.Step 2: Use $size to count filtered tags
$sizecounts how many tags matched the condition.Final Answer:
{ $size: { $filter: { input: "$tags", as: "tag", cond: { $regexMatch: { input: "$$tag", regex: /^a/ } } } } } -> Option DQuick Check:
Filter then count = correct approach [OK]
- Using $arrayElemAt instead of $filter
- Filtering with exact match instead of regex
- Counting whole array without filtering
Ready to prove it?
Pass the test and this pattern moves unknown
