The $filter operator returns a new array with only the elements that pass a condition. It is an aggregation expression operator — like JavaScript’s Array.filter(), but inside MongoDB pipelines.
01
Array Filtering
Keep matching elements only.
02
Syntax
input, as, cond.
03
$$ Variable
Reference each item with $$as.
04
$project Stage
Shape arrays in output.
05
Use Cases
Tags, line items, scores.
06
Optional Limit
Cap how many matches return.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $filter operator selects elements from an array based on a boolean condition. You provide the source array (input), a variable name (as), and a condition (cond). MongoDB loops through each element and keeps only those where cond evaluates to true.
💡
Beginner Tip
Inside cond, reference the current array element with a double dollar sign: if as is "item", use "$$item". A single $ refers to document fields; $$ refers to variables defined in the expression.
Foundation
📝 Syntax
The $filter operator takes an object with input, as, and cond:
input — the array to filter (field path like "$tags" or a literal array).
as — variable name for the current element (e.g. "item" or "tag").
cond — boolean expression; element is kept when this is true.
limit — optional; stops after collecting this many matching elements.
Use inside stages like $project, $addFields, or $set.
⚠️
$filter vs $match
$filter works on arrays inside a document and returns a smaller array. $match works on whole documents and removes documents from the pipeline. Use $filter when you need to trim embedded lists without dropping the parent document.
Even if more than two tags match, MongoDB stops after collecting two results. This is helpful for previews, pagination-style trimming, or performance when you only need a sample.
Applications
🚀 Use Cases
Tag and category cleanup — keep only relevant labels from a product or article tag list.
Order line items — return only in-stock or non-cancelled items from an embedded array.
Student or employee lists — filter nested records by score, status, or role.
API response shaping — trim large arrays in $project before sending data to clients.
🧠 How $filter Works
1
MongoDB reads the input array
The input expression resolves to an array — usually a field like "$tags" or "$items".
Input
2
Each element is tested
MongoDB assigns the current element to the as variable and evaluates cond using $$as.
Loop
3
Matching elements are collected
When cond is true, the element is added to the result array. Optional limit stops early.
Output
=
📊
A trimmed array
You get a new array with only the elements that passed the condition — the original field stays unchanged unless you overwrite it.
Wrap Up
Conclusion
The $filter operator is essential when documents store embedded arrays and you need to return only the relevant items. It keeps parent documents intact while shaping nested data — perfect for tags, line items, scores, and nested user lists.
For beginners, remember three parts: input (the array), as (the loop variable name), and cond (the test using $$variable). Place it inside $project or $addFields and you have MongoDB’s built-in array filtering.
Filter huge arrays without considering performance
Use $filter as a top-level query operator outside expressions
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $filter
Use these points when filtering arrays in aggregation pipelines.
5
Core concepts
🔢01
Array Filtering
Keep matching elements.
Purpose
📝02
Three Parts
input, as, cond.
Syntax
🛠03
$$ Variable
Reference each item.
Usage
🔍04
Nested Data
Tags, items, scores.
Use case
⚠05
Not $match
Filters arrays, not docs.
Edge case
❓ Frequently Asked Questions
$filter returns a new array containing only the elements from an input array that satisfy a condition. It is an aggregation expression operator used inside stages like $project and $addFields.
The syntax is { $filter: { input: <array>, as: <string>, cond: <expression> } }. Optionally add limit: <number> to cap how many matching elements are returned.
Use the variable name you set in as with a double dollar prefix. If as is "item", reference the current element as "$$item" inside cond.
$filter returns null when input is null or refers to a missing field. It does not throw an error.
Use $filter inside expression stages such as $project, $addFields, and $set when you need to keep only matching items from an embedded array.