$regexMatch (Regex Test)

Last updated: 2026-10-09

$regexMatch (Regex Test)

$regexMatch tests whether a string matches a regex and returns true or false — the lightest regex operator, ideal for filtering and data validation. It only answers "does it match?" and returns no content; use $regexFind to extract matches. options accepts flags like i, m, s, and x. Null or missing input yields null.

CategoryString
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $regexMatch: { input: <string-expression>, regex: <pattern>, options: <options> } }

⚙️ Parameters

inputRequired; the string to test.
regexRequired; a Perl-compatible regex pattern string.
optionsOptional flags such as i, m, s, x.

Returns:true if it matches, false otherwise; null if input is null.

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $regexMatch: is the email a Gmail address (case-insensitive)
 db.users.aggregate([
  { $project: { isGmail: { $regexMatch: { input: '$email', regex: '@gmail\\.com$', options: 'i' } } } }
])

Output:

{ "_id": 1, "isGmail": true }
{ "_id": 2, "isGmail": false }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QCan it be used inside $match?
AYes — wrap it in $expr.
QDoes it only return a boolean?
AYes — true or false, with no match content.
QHow to ignore case?
APass 'i' in options.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏