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:


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.