- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $regexMatch (aggregation)
$regexMatch (aggregation)¶
On this page
Definition¶
-
$regexMatch
¶ New in version 4.2.
Performs a regular expression (regex) pattern matching and returns:
true
if a match exists.false
if a match doesn’t exist.
MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.41 with UTF-8 support.
Prior to MongoDB 4.2, aggregation pipeline can only use the query operator
$regex
in the$match
stage. For more information on using regex in a query, see$regex
.
Syntax¶
The $regexMatch
operator has the following syntax:
Field | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
input | The string on which you wish to apply the regex pattern. Can be a string or any valid expression that resolves to a string. |
||||||||||
regex | The regex pattern to apply. Can be any valid expression that resolves to either a string or regex
pattern
Alternatively, you can also specify the regex options with the
options field. To specify the You cannot specify options in both the |
||||||||||
options | Optional. The following Note You cannot specify options in both the
|
Returns¶
The operator returns a boolean:
true
if a match exists.false
if a match doesn’t exist.
See also
Behavior¶
$regexMatch
and Collation¶
$regexMatch
ignores the collation specified for the
collection, db.collection.aggregate()
, and the index, if used.
For example, the create a sample collection with collation strength
1
(i.e. compare base character only and ignore other differences
such as case and diacritics):
Insert the following documents:
Using the collection’s collation, the following operation performs a case-insensitive and diacritic-insensitive match:
The operation returns the following 3 documents:
However, the aggregation expression $regexMatch
ignores
collation; that is, the following regular expression pattern matching examples
are case-sensitive and diacritic sensitive:
Both operations return the following:
To perform a case-insensitive regex pattern matching, use the i Option instead. See i Option for an example.
Examples¶
$regexMatch
and Its Options¶
To illustrate the behavior of the $regexMatch
operator as
discussed in this example, create a sample collection products
with
the following documents:
By default, $regexMatch
performs a case-sensitive match.
For example, the following aggregation performs a case-sensitive
$regexMatch
on the description
field. The regex
pattern /line/
does not specify any grouping:
The operation returns the following:
The following regex pattern /lin(e|k)/
specifies a grouping
(e|k)
in the pattern:
The operation returns the following:
i
Option¶
Note
You cannot specify options in both the regex
and the
options
field.
To perform case-insensitive pattern matching, include the i option as part of the regex field or in the options field:
For example, the following aggregation performs a case-insensitive
$regexMatch
on the description
field. The regex
pattern /line/
does not specify any grouping:
The operation returns the following documents:
m
Option¶
Note
You cannot specify options in both the regex
and the
options
field.
To match the specified anchors (e.g. ^
, $
) for each line of a
multiline string, include the m option
as part of the regex field or in the
options field:
The following example includes both the i
and the m
options to
match lines starting with either the letter s
or S
for
multiline strings:
The operation returns the following:
x
Option¶
Note
You cannot specify options in both the regex
and the
options
field.
To ignore all unescaped white space characters and comments (denoted by
the un-escaped hash #
character and the next new-line character) in
the pattern, include the s option in the
options field:
The following example includes the x
option to skip unescaped white
spaces and comments:
The operation returns the following:
s
Option¶
Note
You cannot specify options in both the regex
and the
options
field.
To allow the dot character (i.e. .
) in the pattern to match all
characters including the new line character, include the s option in the options field:
The following example includes the s
option to allow the dot
character (i.e. .) to match all characters including new line as well
as the i
option to perform a case-insensitive match:
The operation returns the following:
Use $regexMatch
to Check Email Address¶
Create a sample collection feedback
with the following documents:
The following aggregation uses the $regexMatch
to check
if the comment
field contains an email address with
@mongodb.com
and categorize the feedback as Employee
or
External
.
The operation returns the following documents: