Search⌘ K
AI Features

Querying with Regular Expressions

Explore how to use regular expressions in MongoDB to perform advanced text searches. Understand regex syntax, metacharacters, and flags to filter and match data patterns effectively in your collections.

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 ...