The $regexMatch operator checks whether a regular expression pattern appears in a string and returns true or false. Use it for validation, filtering, and conditional logic — without extracting match text.
01
Boolean Test
true or false.
02
Same Syntax
input + regex.
03
$match Stage
Filter in pipelines.
04
$cond Logic
Branch on pattern.
05
Validation
Email, SKU, format.
06
vs $regexFind
Check vs extract.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $regexMatch operator tests whether a regular expression pattern matches anywhere in a string. The result is a boolean: true if at least one match exists, false otherwise.
Unlike $regexFind and $regexFindAll, $regexMatch does not return matched text, positions, or capture groups. It answers a simple question: “Does this pattern appear in the string?”
💡
Beginner Tip
Use $regexMatch inside the aggregation $match stage to filter documents by a string pattern. Use $project to add a validation flag like isValidEmail: true.
Foundation
📝 Syntax
The $regexMatch operator shares the same argument shape as $regexFind:
\d{3}-\d{3}-\d{4} matches a simple ###-###-#### format. Use $regexMatch for quick format checks; use $regexFind if you need to extract the digits.
Applications
🚀 Use Cases
Format validation — flag emails, phone numbers, SKUs, or IDs that match a pattern.
Pipeline filtering — use $match + $expr to keep documents whose fields match a regex.
Conditional fields — branch with $cond based on pattern presence.
Data quality reports — add boolean columns for auditing without extracting match text.
🧠 How $regexMatch 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 searches for a match
MongoDB scans the string for at least one substring that satisfies the pattern. It stops as soon as a match is found.
Search
3
A boolean is returned
true if a match exists; false if not. No match text or position is included.
Result
=
✅
true or false
Use in $match, $cond, or store as a validation flag.
Wrap Up
Conclusion
The $regexMatch operator tests whether a regular expression pattern appears in a string and returns true or false. It is the right choice when you need validation or filtering without extracting match details.
Pair it with $match and $expr to filter documents, or with $cond for branching. For matched text, use $regexFind or $regexFindAll. Next in the series: $reverseArray.
Wrap in $expr when using inside aggregation $match
Use options: "i" for case-insensitive checks
Prefer $regexFind when you need the matched substring
Keep patterns readable and test them in mongosh first
❌ Don’t
Expect match text or idx from $regexMatch
Use $regexMatch without $expr in aggregation $match
Compare $regexFind to null when a boolean suffices
Rely on regex alone for strict email/phone validation in production
Confuse $regexMatch with query $regex
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $regexMatch
Use these points when testing patterns in aggregation pipelines.
5
Core concepts
✅01
Boolean
true or false.
Purpose
📝02
input + regex
Same shape as $regexFind.
Syntax
🛠03
No extraction
Check only.
Output
🗃04
$expr + $match
Filter documents.
Filtering
⚠05
false
null input or no match.
Edge case
❓ Frequently Asked Questions
$regexMatch tests whether a regular expression pattern matches anywhere in a string and returns true or false. It does not return the matched text — only a boolean result.
The syntax is { $regexMatch: { input: <string>, regex: <pattern>, options: <options> } }. Use inside $match, $project, $addFields, $set, or $cond. The options field is optional.
A boolean: true when the pattern matches at least once in the input string, false when it does not. If input is null, the result is false.
$regex is a query filter operator used in find() and $match to include or exclude whole documents. $regexMatch is an aggregation expression that evaluates a single string field and returns true or false.
Use $regexMatch when you only need to know if a pattern exists (validation, filtering, flags). Use $regexFind or $regexFindAll when you need the matched text, position, or capture groups.