The $regexFindAll operator finds every regular expression match in a string and returns them as an array. Use it when you need all hashtags, mentions, numbers, or tokens — not just the first one.
01
All Matches
Every regex hit.
02
Array Output
Match objects list.
03
idx + captures
Position and groups.
04
Aggregation
$project / $addFields.
05
Use Cases
Tokenize, count.
06
vs $regexFind
All vs first only.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $regexFindAll operator searches a string for a regular expression pattern and returns an array of match objects. Each object contains match (matched text), idx (start index), and captures (parenthesized groups).
When no matches are found, the result is an empty array[] — not null. This makes it easy to chain with $size, $map, or $filter without extra null checks.
💡
Beginner Tip
Need only the first match? Use $regexFind instead. Need only yes/no? Use $regexMatch. Use $regexFindAll when you want every occurrence with its text and position.
Foundation
📝 Syntax
The $regexFindAll operator takes the same shape as $regexFind:
The pattern #\w+ matches each hash symbol plus word characters. Unlike $regexFind, all matches are returned. Posts with no hashtags get an empty array.
📈 Practical Patterns
Count matches, flatten to strings, and extract @mentions.
\d+ matches one or more digits. $toInt converts each matched string to a number for further math or filtering.
Applications
🚀 Use Cases
Token extraction — collect all hashtags, mentions, or keywords from free text.
Match counting — measure how often a pattern appears with $size.
Log parsing — pull every error code, IP address, or timestamp from messages.
Data normalization — build string arrays for tags, categories, or identifiers in ETL pipelines.
🧠 How $regexFindAll Works
1
MongoDB evaluates input and regex
The pipeline resolves the string (input) and compiles the pattern (regex) with any options.
Input
2
The engine scans the full string
MongoDB searches left to right and records every non-overlapping substring that satisfies the pattern.
Search
3
Each match becomes an array element
Every hit is stored as { match, idx, captures }. No matches produce [].
Collect
=
🔎
Array of all matches
Use $map, $size, or $filter on the result array.
Wrap Up
Conclusion
The $regexFindAll operator finds every regular expression match in a string and returns them as an array of { match, idx, captures } objects. When nothing matches, you get [].
Pair it with $map to flatten results or $size to count them. For a single match, use $regexFind. For a boolean check, use $regexMatch. Next in the series: $regexMatch.
Use capture groups when extracting a specific part of each match
Handle empty arrays naturally (no null check needed)
Use $regexMatch for simple existence checks
❌ Don’t
Expect null for no match — the result is []
Use $regexFindAll as a query filter (it is expression-only)
Run greedy patterns on very large strings without testing performance
Assume overlapping matches are returned (MongoDB uses non-overlapping search)
Confuse $regexFindAll with query $regex
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $regexFindAll
Use these points when extracting every regex match in pipelines.
5
Core concepts
🔎01
All Matches
Every occurrence.
Purpose
📝02
input + regex
Same as $regexFind.
Syntax
🛠03
Array output
List of objects.
Output
🗃04
$map / $size
Flatten or count.
Chaining
⚠05
[]
No matches found.
Edge case
❓ Frequently Asked Questions
$regexFindAll searches a string for every regular expression match and returns an array of objects. Each object has match (matched text), idx (start position), and captures (capture groups). If nothing matches, it returns an empty array.
The syntax is { $regexFindAll: { input: <string>, regex: <pattern>, options: <options> } }. Use inside $project, $addFields, or $set. The options field is optional.
An array like [{ match: "...", idx: number, captures: [...] }, ...]. When there are no matches, the result is [] (an empty array), not null.
$regexFind returns only the first match as a single object or null. $regexFindAll returns every match in the string as an array of match objects.
Use $regexFindAll when you need the matched text, positions, or capture groups for every occurrence. Use $regexMatch when you only need a true/false answer about whether a pattern exists.