Visit complete MongoDB roadmap

← Back to Topics List

Regular Expression

In MongoDB, regular expressions (regex) are a powerful data type that allows you to search for patterns within text strings. They can be used in query operations to find documents that match a specific pattern and are particularly useful when working with text-based data or when you don’t have an exact match for your query.

Creating a Regular Expression

In MongoDB, you can create a regular expression using the /pattern/flags syntax or by using the BSON type RegExp. Here’s an example:

// Creating a regex to find documents containing the word 'example'
var regex = /example/i; // Using JavaScript regex syntax with 'i' flag (case-insensitive)
var bsonRegex = new RegExp('example', 'i'); // Using BSON RegExp type

Both methods will result in the same regex pattern, with the i flag indicating case-insensitivity.

Querying with Regular Expressions

You can use regular expressions in MongoDB queries using the $regex operator or by directly passing the regex pattern:

db.collection.find({ field: /example/i }); // Using plain regex pattern
db.collection.find({ field: { $regex: /example/i } }); // Using $regex operator

Regular Expression Flags

MongoDB supports the following regex flags to provide flexibility in pattern matching:

  • i: Case-insensitive match
  • m: Multi-line match
  • x: Ignore whitespace and comments in the pattern
  • s: Allow . to match all characters, including newlines


db.collection.find({ field: { $regex: /example/im } }); // Case-insensitive and multi-line match

Escaping Special Characters

In regex patterns, certain characters have special meanings, such as . (matches any character), * (matches zero or more repetitions). To search for a literal character that has a special meaning in regex, you must escape it with a backslash (\):

db.collection.find({ field: /example\.com/i }); // Search for ''

Regular expressions in MongoDB allow you to search for complex patterns within text strings effectively. By understanding the basic syntax and flags, you can enhance your querying capabilities to find the exact data you need.

Community is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© · FAQs · Terms · Privacy


The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.