The $regex operator filters documents where a string field matches a regular expression pattern. Use it for partial text search, prefix filters, email domain checks, and flexible pattern matching in find() and $match.
01
Pattern Match
Regex on strings.
02
$regex + $options
Pattern and flags.
03
Case Insensitive
Option i.
04
^ and $
Start / end anchors.
05
Use Cases
Search, filter, validate.
06
vs $text
Regex vs full-text.
Fundamentals
Definition and Usage
In MongoDB, the $regex operator performs pattern matching on string fields. A document matches when the field value satisfies the regular expression. For example, { name: { $regex: "john", $options: "i" } } finds names containing “john” in any case.
$regex is a query operator used in find() filters and $match stages. It does not transform field values — it only includes or excludes documents based on whether the pattern matches.
💡
Beginner Tip
In mongosh you can write a shorthand: { name: /^john/i }. In application drivers, prefer the explicit { $regex: "pattern", $options: "i" } form for clarity and portability.
Foundation
📝 Syntax
The $regex operator is applied to a field with a pattern and optional flags:
Combine $or with $regex when the search term might appear in different fields.
Applications
🚀 Use Cases
Search boxes — partial, case-insensitive name or title lookups.
SKU / code prefixes — filter product or order codes starting with a pattern.
Email validation — rough domain or format checks with anchored patterns.
Data cleanup — exclude test, demo, or internal accounts with $not + $regex.
🧠 How $regex Works
1
MongoDB evaluates the pattern
The regex engine compiles the pattern and options (i, m, etc.) for each query.
Input
2
Each document’s field is tested
MongoDB checks whether the field string matches the pattern. Non-string or missing fields typically do not match.
Match
3
Matching documents are returned
In find() or $match, only documents that pass the regex test continue in the result set.
Output
=
🔍
Flexible text filtering
Pattern-based queries without exact equality on full strings.
Wrap Up
Conclusion
The $regex operator brings regular expression pattern matching to MongoDB queries. Use it for substring search, prefix and suffix filters, and exclusions with $not. Remember to escape special characters and choose patterns that work with your indexes when performance matters.
For extracting matched text inside pipelines, use $regexFind. Next in the series: $regexFind.
Forget to escape . when matching literal dots (e.g. domains)
Use overly complex regex on unindexed fields at high volume
Confuse query $regex with aggregation $regexFind
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $regex
Use these points when writing pattern-matching queries in MongoDB.
5
Core concepts
🔍01
Pattern Match
Regex on strings.
Purpose
📝02
$regex + $options
Pattern and flags.
Syntax
🛠03
^ and $
Start / end anchors.
Patterns
🚫04
+$not
Exclude matches.
Exclusion
⚠05
Index Aware
^prefix helps.
Performance
❓ Frequently Asked Questions
$regex filters documents where a string field matches a regular expression pattern. Use it in find() and $match to search by partial text, prefixes, suffixes, or complex patterns.
The syntax is { field: { $regex: "pattern", $options: "i" } }. You can also use a BSON regex literal shorthand like { field: /^pattern/i } in mongosh.
Common options: i = case insensitive, m = multiline, x = extended (ignore whitespace in pattern), s = dot matches newline. Combine them like $options: "im".
Prefix patterns anchored at the start (like ^john) can use indexes when case-sensitive and not using the i option. Leading wildcards (.*john) or case-insensitive searches often require collection scans.
$regex is a query filter that returns matching documents. $regexFind is an aggregation expression that extracts the matched substring from a field value inside a pipeline.