The $regexFind operator finds the first regular expression match in a string and returns match details. Use it in aggregation pipelines to extract emails, hashtags, codes, or any substring without filtering entire documents.
01
Extract Match
First regex hit.
02
match + idx
Text and position.
03
Captures
Capture groups.
04
Aggregation
$project / $addFields.
05
Use Cases
Parse, validate.
06
vs $regex
Extract vs filter.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $regexFind operator searches a string for a regular expression pattern and returns metadata about the first match. The result is an object with match (matched text), idx (start index), and captures (parenthesized groups). If nothing matches, the result is null.
Unlike query $regex, which filters documents, $regexFindtransforms data inside pipelines. It is ideal for parsing log messages, extracting domains from emails, or pulling SKU prefixes into separate fields.
💡
Beginner Tip
Access the matched text with $matchResult.match after projecting the full $regexFind result, or use dot notation in a nested $project. Need all matches? Use $regexFindAll instead.
Foundation
📝 Syntax
The $regexFind operator takes an object with input, regex, and optional options:
Alternation (PRO|DEV|TEST) matches any of those prefixes. The i option allows case-insensitive matching.
Applications
🚀 Use Cases
String parsing — extract hashtags, mentions, or codes from free text.
Email / URL parsing — pull domains, usernames, or TLDs with capture groups.
Log analysis — extract error codes or timestamps from message fields.
Data enrichment — add derived fields in ETL pipelines without application-side regex.
🧠 How $regexFind 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 finds the first match
MongoDB scans the string left to right and stops at the first substring that satisfies the pattern.
Search
3
Match metadata is returned
On success: match, idx, and captures. On failure: null.
Output
=
🔎
Extracted substring data
Use .match or captures in downstream pipeline stages.
Wrap Up
Conclusion
The $regexFind operator extracts the first regular expression match from a string inside MongoDB aggregation pipelines. It returns match, idx, and captures — or null when nothing matches.
Use it for parsing and enrichment; use query $regex to filter documents. For all matches, switch to $regexFindAll. Next in the series: $regexFindAll.
Use capture groups when you need a specific part of the match
Read .match or captures[0] in a follow-up projection
Use $regexMatch when you only need true/false
Escape special regex characters in literal patterns
❌ Don’t
Expect all matches — use $regexFindAll for that
Use $regexFind as a query filter (it is expression-only)
Assume captures is always non-empty (depends on groups)
Run complex regex on huge strings without considering performance
Confuse $regexFind with query $regex
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $regexFind
Use these points when extracting regex matches in pipelines.
5
Core concepts
🔎01
First Match
One result only.
Purpose
📝02
input + regex
String and pattern.
Syntax
🛠03
match / idx
Text + position.
Output
🗃04
captures[]
Group extraction.
Groups
⚠05
null
No match found.
Edge case
❓ Frequently Asked Questions
$regexFind searches a string for the first regular expression match and returns an object with match (the matched text), idx (start position), and captures (capture groups). If no match is found, it returns null.
The syntax is { $regexFind: { input: <string>, regex: <pattern>, options: <options> } }. Use inside $project, $addFields, or $set. The options field is optional.
On success: { match: "...", idx: number, captures: [...] }. On failure: null. The match field contains the first matched substring; idx is the zero-based start index.
$regex is a query filter that includes or excludes whole documents. $regexFind is an aggregation expression that extracts the matched portion of a string field and returns match metadata.
$regexFind returns only the first match as a single object (or null). $regexFindAll returns an array of all matches in the string.