$regex

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Query filter operators

What it does

Matches strings using regular expressions.

Syntax

{ field: { $regex: pattern, $options: flags } }

Use case

Pattern matching and cleanup.

Important notes

Unanchored/case-insensitive regex may be inefficient.

$regex: practical example
db.users.find({ email: { $regex: "@example\\.com$", $options: "i" } })
💡 Explanation Matches strings using regular expressions. When to use it Pattern matching and cleanup. Caution Unanchored/case-insensitive regex may be inefficient.
Key Takeaways
  • Matches strings using regular expressions.
  • Typical use: Pattern matching and cleanup.