Search⌘ K
AI Features

Querying with Regular Expressions

Understand how to apply regular expressions in MongoDB queries to perform pattern matching and text searches. Learn regex syntax, metacharacters, and flags to filter documents efficiently and create advanced search criteria.

What are regular expressions?

Regular expressions, also known as regex, are special text strings that define a search pattern. They can be used to search, match, and manipulate text within documents. MongoDB supports JavaScript-style regular expressions, making them both familiar and highly versatile.

Syntax:

Javascript (babel-node)
db.collection.find({ field: /pattern/ })

Here, field is the name of the field we want to search, and /pattern/ is the regex pattern we want to match. The pattern is enclosed between / slashes.

Components of a regular expression

Regex are not just random ...