$regex is a MongoDB query operator that keeps documents whose string field matches a regular expression. Use it in find() or $match for partial text, prefixes, suffixes, and simple patterns.
Remember
$regex = field matches this pattern?
{ field: { $regex: "pattern", $options: "i" } }
// mongosh shorthand
{ field: /pattern/i }
^Note → starts with Note
book$ → ends with book
ash → contains ash
i → case insensitive
query filter — not an aggregation extract
(for extract → $regexFind)
Write a pattern against a string field, then optionally add flags.
Choose the string field to test (for example name).
Pass the pattern with { $regex: "..." } (or a /.../ literal).
Add $options when you need flags such as "i".
Prefer anchored prefixes (^...) when you care about index use.
Piece
Required?
Role
Field
Yes
String field to match
$regex pattern
Yes
Regular expression text
$options
No
Flags like i, m, s, x
Pseudocode
Pseudocode
for each document:
text = document.field
if text is a string AND text matches pattern (with options):
keep document
else:
skip
Reference
Cheat Sheet
Keep these facts handy while writing pattern filters.
Topic
Detail
Type
Query operator (find / $match)
Syntax
{ field: { $regex: "...", $options: "i" } }
Shorthand
{ field: /pattern/i }
Common flags
i case · m multiline · s dotall
Prefix tip
^Note can use an index
vs $regexFind
Filter docs vs extract match
Sample
ash + i matches "Asha"
Try it
Live Preview
Test simple patterns against sample product names (same idea as $regex).
Simulates { name: { $regex: "...", $options: "..." } } on three products.
Result
pattern = /ash/i
Notebook → match
Pen Set → no
Flashcard → match
kept → Notebook, Flashcard
Trace
Worked Walkthrough — Names with “ash”
Three products sit in a catalog. We want names that contain ash, ignoring case.
Calculation
Documents:
Notebook
Pen Set
Flashcard
Filter:
{ name: { $regex: "ash", $options: "i" } }
Notebook → contains "ash" ✓
Pen Set → no "ash" ✗
Flashcard → contains "ash" ✓
Result → Notebook, Flashcard
Code
MongoDB Examples
We use three products. First insert them, then filter with $regex, then show a prefix match. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine three products: Notebook, Pen Set, and Flashcard. We save them in a collection named products.
Index-friendly shape. Prefix patterns (without i) are the best $regex form for indexes.
Edge Cases & Pitfalls
Check these before shipping regex filters to production.
index
Leading wildcards are slow
Patterns like .*ash or unanchored contains often scan the collection. Prefer ^prefix when you can.
case
Option i has a cost
Case-insensitive $regex usually cannot use a normal index efficiently. Use it when you truly need it.
escape
Escape special characters
Characters like ., *, +, and ( are regex metacharacters. Escape user input before building a pattern.
vs find
Filter ≠ extract
$regex decides which documents to keep. To pull out the matched text, use $regexFind.
Analysis
Performance Notes
Approach
Cost
Best for
^prefix (no i)
Can use index
Starts-with filters
Contains / i
Often COLLSCAN
Small collections / rare queries
$text + text index
Index-backed search
Full-text search needs
Regex is flexible but easy to make expensive. Anchor prefixes when possible, and consider $text for real search workloads.
Remember
Key Takeaways
Role: keep docs whose string field matches a pattern.
Flags: use $options (for example "i").
Speed:^prefix helps indexes; contains / i often does not.
Sample:ash + i → Notebook, Flashcard.
One line:$regex answers “does this string field match my pattern?”—and returns the matching documents.
Frequently Asked Questions
$regex filters documents where a string field matches a regular expression pattern. Use it in find() and $match for partial text, prefixes, suffixes, or richer patterns.
Use { field: { $regex: "pattern", $options: "i" } }. In mongosh you can also write a RegExp literal such as { field: /^pattern/i }.
Common flags: i = case insensitive, m = multiline, x = extended (ignore unescaped whitespace in the pattern), s = dot matches newline. Combine them like "im".
Case-sensitive prefix patterns anchored at the start (like ^Note) can use an index. Leading wildcards or the i option often force a collection scan.
$regex is a query filter that returns matching documents. $regexFind is an aggregation expression that extracts match details from a string inside a pipeline.
Use $text for full-text search with a text index. Use $regex for simple pattern checks on one field when a text index is not the right tool.