$regexMatch is a MongoDB aggregation expression that tests whether a regular expression matches a string and returns true or false. It does not return the matched text—only a yes/no answer.
Remember
$regexMatch ≈ /pattern/.test(string)
{ $regexMatch: {
input: <string>,
regex: <pattern>,
options: <options> // optional
} }
match exists → true
no match → false
case-sensitive by default (use options: "i")
Set input to a string field (for example "$text").
Set regex to a pattern string or /pattern/.
Optional: add options such as "i", or filter with $expr inside $match.
Piece
Required?
Role
input
Yes
String to test
regex
Yes
Pattern as string or /.../
options
No
i, m, x, s (not both here and on the regex)
Pseudocode
Pseudocode
for each document:
s = evaluate(input)
pattern = evaluate(regex)
if pattern matches anywhere in s:
result = true
else:
result = false
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while writing boolean regex tests.
Topic
Detail
Type
Aggregation expression (boolean)
Syntax
{ $regexMatch: { input, regex, options? } }
Result
true or false
Default
Case-sensitive; collation ignored
Case-insensitive
options: "i" or /pattern/i
Filter docs
$match: { $expr: { $regexMatch: { ... } } }
Need match text?
Use $regexFind / $regexFindAll
Try it
Live Preview
Text: "Email me at [email protected]". Pick a pattern and see true or false.
Simulates $regexMatch boolean results (same idea as the mongosh examples).
Result
input = "Email me at [email protected]"
regex = /@mongodb\.com/i
$regexMatch → true
{ matched: true }
Trace
Worked Walkthrough — Phone vs Email
Same idea as /pattern/.test(text) in JavaScript: does the pattern appear anywhere?
Calculation
text = "Call 555-0199 today"
/\d{3}-\d{4}/.test(text) → true (phone found)
/@mongodb\.com/i.test(text) → false (no email)
text2 = "Email me at [email protected]"
/@mongodb\.com/.test(text2) → false (case-sensitive: MongoDB ≠ mongodb)
/@mongodb\.com/i.test(text2) → true (i ignores case)
$regexMatch returns only the boolean — not the match text.
Code
MongoDB Examples
We use a small notes collection with a text field. First we flag phone-like patterns, then we categorize notes by email with a case-insensitive check. Insert the sample data once in mongosh.
Example 1 — Sample Data
Imagine three short notes. Each has an id and a text string. We save them in a collection named notes.
mongosh
db.notes.insertMany([
{ _id: 1, text: "Call 555-0199 today" },
{ _id: 2, text: "No phones here" },
{ _id: 3, text: "Email me at [email protected]" }
]);
How the Data Is Stored in MongoDB
After the insert, MongoDB keeps three documents in notes. Run find() to see them:
mongosh
db.notes.find()
Stored in MongoDB
[
{ _id: 1, text: "Call 555-0199 today" },
{ _id: 2, text: "No phones here" },
{ _id: 3, text: "Email me at [email protected]" }
]
Put flags either on the regex literal (/pat/i) or in options—not both. Use options for s / x.
case
Collation is ignored
Default matching is case-sensitive. Use i when you want case-insensitive checks.
query
Filtering documents
In aggregation, wrap with $match + $expr. For simple find filters, $regex is often enough.
Analysis
Performance Notes
Approach
Cost
Best for
$regexMatch
Can stop at first hit
Flags, validation, branching
$regexFind / $regexFindAll
More work + larger output
When you need match details
Query $regex
Filter at query time
Simple document filters in find()
Prefer $regexMatch when a boolean is enough—it is lighter than collecting match objects. Still keep patterns focused, and filter early with $match when only some rows need the test.
Remember
Key Takeaways
Role: regex existence test → true / false.
Not details: no match / idx / captures.
Case: default sensitive; use i when needed.
Pair with:$cond for labels; $expr for filters.
One line:$regexMatch answers “does this pattern appear?”—true or false, nothing more.
Frequently Asked Questions
$regexMatch tests whether a regular expression matches anywhere in a string and returns true or false. It does not return the matched text—only a boolean.
Use { $regexMatch: { input: <string>, regex: <pattern>, options: <options> } } inside stages like $project, $addFields, $cond, or $match with $expr. The options field is optional.
$regex is a query filter used in find() / $match to keep whole documents. $regexMatch is an aggregation expression that evaluates one string and returns true or false.
Use $regexMatch when you only need to know if a pattern exists. Use $regexFind or $regexFindAll when you need the matched text, position, or capture groups.
No. $regexMatch ignores collation and is case-sensitive by default. Use the i option for case-insensitive matching.
If there is no match—including when the input does not contain the pattern—the result is false. Prefer guarding missing fields when needed.