Regular Expression Values

MongoDB supports regular-expression matching for string fields. Regular expressions can be used to search for text patterns rather than only exact values.

For example, suppose a collection contains:

{ name: "Arun Kumar" }
{ name: "Aruna" }
{ name: "Meera" }

A prefix pattern can find names beginning with Arun:

db.users.find({ name: /^Arun/ });

Common regular-expression patterns

  • ^Arun matches strings beginning with Arun.
  • Kumar$ matches strings ending with Kumar.
  • ^Arun.*Kumar$ matches strings beginning with Arun and ending with Kumar, with any sequence between those parts.

Regular expressions can be powerful, but they should be designed carefully. Complex patterns may consume substantial resources, especially when they cannot use a suitable index efficiently.

Case-insensitive matching

db.users.find({ name: /^arun/i });

The i option requests case-insensitive matching.

A case-insensitive regular expression does not automatically benefit from a case-insensitive collation index in the same way as an equality query using collation. Index behavior depends on the pattern and index configuration.

Prefix expressions and indexes

A suitable index can improve some anchored prefix searches, particularly simple case-sensitive prefix expressions.

For example:

db.users.createIndex({ name: 1 });

Then inspect a query plan:

db.users.find({ name: /^Arun/ }).explain("executionStats");

An index does not guarantee that every regular expression query is efficient. Test with representative data and inspect the execution plan.

Avoid unnecessary unanchored patterns

A pattern such as /run/ searches for the substring run anywhere in the value. It may require examining many candidate strings because it does not provide the same index bounds as a simple anchored prefix pattern.

Security considerations

If a user supplies a search term, avoid constructing an unrestricted regular expression directly from that input. Escape literal user input when the intention is a literal substring or prefix search, and consider search-specific mechanisms for more complex requirements.

Search strings using regular expressions
db.regexExamples.insertMany([
 { name: "Arun Kumar" },
 { name: "Aruna" },
 { name: "Meera" }
]);

// Case-sensitive prefix search.
db.regexExamples.find({ name: /^Arun/ });

// Case-insensitive prefix search.
db.regexExamples.find({ name: /^arun/i });

// Create an index for suitable prefix searches.
db.regexExamples.createIndex({ name: 1 });

// Inspect query execution.
db.regexExamples.find({ name: /^Arun/ }).explain("executionStats");
💡 The first two filters use anchored prefix patterns. The final query requests execution statistics so you can inspect how MongoDB executes the search. Index effectiveness depends on the expression, options, index configuration, and actual data distribution.
Key Takeaways
  • Regular expressions can match string patterns.
  • Anchored prefix patterns can be more index-friendly than unanchored substring searches.
  • The i option requests case-insensitive matching.
  • An index does not guarantee an efficient regular-expression query.
  • Escape user-supplied input when it should be treated as literal text.
Knowledge Check

Why can `/run/` be less index-friendly than `/^run/`?