Hướng dẫn mongodb eq regex
Docs Home → MongoDB Manual For data hosted on MongoDB Atlas, MongoDB offers a full-text search solution, MongoDB Atlas Search. If you frequently run
case-insensitive regex queries (utilizing the Atlas Search queries significiantly improve the performance of text queries and offer more options for customizing query parameters. To learn more, see MongoDB Atlas Search. For self-managed deployments, you can find information on our regex capability below. $regex Provides regular expression capabilities for pattern matching strings in queries. MongoDB uses Perl compatible regular expressions (i.e. "PCRE" ) version 8.42 with UTF-8 support. To use
In MongoDB, you can also use regular expression objects (i.e.
For restrictions on particular syntax use, see $regex vs. /pattern/ Syntax. $options The following
NoteThe To include a regular expression in an
You cannot use To include a regular expression in a comma-separated list of query conditions for the field, use the
To use either the
To use PCRE supported features in a regular expression that aren't
supported in JavaScript, you must use the To match case-insensitive strings:
For example, the regular expression
These strings match the example regular expression:
The following example uses the
Starting in 4.0.7,
In 4.0.6 and earlier, you could use For case sensitive regular expression queries, if an index exists for the field, then MongoDB matches the regular expression against the values in the index, which can be faster than a collection scan. Further optimization can occur if the regular expression is a "prefix expression", which means that all potential matches start with the same string. This allows MongoDB to construct a "range" from that prefix and only match against those values from the index that fall within that range. A regular expression is a "prefix expression" if it starts with a caret ( Additionally,
while Case insensitive regular expression queries generally cannot use indexes effectively. The The examples in this section use the following
The following example matches all documents where the
The example is analogous to the following SQL LIKE statement:
Example output:
The following example uses the
Example output:
The following example uses the
Example output:
Without the
If the
Example output:
The following example uses the
Example output:
Without the
The following example uses the
Example output:
The following example
uses the regular expression
Example output:
|