Skip to content

Commit 318e99c

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-24951 regex updates (#1771)
* DOCSP-24951-regex-updates * DOCSP-24951-regex-updates * DOCSP-24951-regex-updates * DOCSP-24951-regex-updates Co-authored-by: jason-price-mongodb <[email protected]>
1 parent a35751a commit 318e99c

File tree

1 file changed

+112
-33
lines changed

1 file changed

+112
-33
lines changed

source/reference/operator/query/regex.txt

Lines changed: 112 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,11 @@ Definition
103103

104104
- Requires ``$regex`` with ``$options`` syntax
105105

106-
107106
.. note::
108107

109108
The :query:`$regex` operator does not support the global search
110109
modifier ``g``.
111110

112-
113111
Behavior
114112
--------
115113

@@ -160,16 +158,32 @@ must use :query:`$options` for both:
160158
PCRE vs JavaScript
161159
``````````````````
162160

163-
To use PCRE supported features in the regex pattern that are
164-
unsupported in JavaScript, you must use the :query:`$regex` operator
165-
expression with the pattern as a string. For example, to use ``(?i)``
166-
in the pattern to turn case-insensitivity on for the remaining pattern
167-
and ``(?-i)`` to turn case-sensitivity on for the remaining pattern, you
168-
must use the :query:`$regex` operator with the pattern as a string:
161+
To use PCRE supported features in a regular expression that aren't
162+
supported in JavaScript, you must use the :query:`$regex` operator and
163+
specify the regular expression as a string.
164+
165+
To match case-insensitive strings:
166+
167+
- ``"(?i)"`` begins a case-insensitive match.
168+
- ``"(?-i)"`` ends a case-insensitive match.
169+
170+
For example, the regular expression ``"(?i)a(?-i)cme"`` matches strings
171+
that:
172+
173+
- Begin with ``"a"`` or ``"A"``. This is a case-insensitive match.
174+
- End with ``"cme"``. This is a case-sensitive match.
175+
176+
These strings match the example regular expression:
177+
178+
- ``"acme"``
179+
- ``"Acme"``
180+
181+
The following example uses the :query:`$regex` operator to find ``name``
182+
field strings that match the regular expression ``"(?i)a(?-i)cme"``:
169183

170184
.. code-block:: javascript
171185

172-
{ name: { $regex: '(?i)a(?-i)cme' } }
186+
{ name: { $regex: "(?i)a(?-i)cme" } }
173187

174188
``$regex`` and ``$not``
175189
```````````````````````
@@ -208,6 +222,7 @@ Index Use
208222
For case sensitive regular expression queries, if an index exists for
209223
the field, then MongoDB matches the regular expression against the
210224
values in the index, which can be faster than a collection scan.
225+
211226
Further optimization can occur if the regular expression is a "prefix
212227
expression", which means that all potential matches start with the same
213228
string. This allows MongoDB to construct a "range" from that prefix and
@@ -232,15 +247,17 @@ and is unable to utilize case-insensitive indexes.
232247
Examples
233248
--------
234249

235-
The following examples use a collection ``products`` with the following
236-
documents:
250+
The examples in this section use the following ``products`` collection:
237251

238252
.. code-block:: javascript
239253

240-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
241-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
242-
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
243-
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
254+
db.products.insertMany( [
255+
{ _id: 100, sku: "abc123", description: "Single line description." },
256+
{ _id: 101, sku: "abc789", description: "First line\nSecond line" },
257+
{ _id: 102, sku: "xyz456", description: "Many spaces before line" },
258+
{ _id: 103, sku: "xyz789", description: "Multiple\nline description" },
259+
{ _id: 104, sku: "Abc789", description: "SKU starts with A" }
260+
] )
244261

245262
Perform a ``LIKE`` Match
246263
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -259,6 +276,17 @@ The example is analogous to the following SQL LIKE statement:
259276
SELECT * FROM products
260277
WHERE sku like "%789";
261278

279+
Example output:
280+
281+
.. code-block:: javascript
282+
:copyable: false
283+
284+
[
285+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
286+
{ _id: 103, sku: 'xyz789', description: 'Multiple\nline description' },
287+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
288+
]
289+
262290
.. _regex-case-insensitive:
263291

264292
Perform Case-Insensitive Regular Expression Match
@@ -272,12 +300,16 @@ with ``ABC``.
272300

273301
db.products.find( { sku: { $regex: /^ABC/i } } )
274302

275-
The query matches the following documents:
303+
Example output:
276304

277305
.. code-block:: javascript
306+
:copyable: false
278307

279-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
280-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
308+
[
309+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
310+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
311+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
312+
]
281313

282314
.. _regex-multiline-match:
283315

@@ -291,18 +323,26 @@ with the letter ``S`` for multiline strings:
291323

292324
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
293325

294-
The query matches the following documents:
326+
Example output:
295327

296328
.. code-block:: javascript
329+
:copyable: false
297330

298-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
299-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
331+
[
332+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
333+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
334+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
335+
]
300336

301-
Without the ``m`` option, the query would match just the following document:
337+
Without the ``m`` option, the example output is:
302338

303339
.. code-block:: javascript
340+
:copyable: false
304341

305-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
342+
[
343+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
344+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
345+
]
306346

307347
If the :query:`$regex` pattern does not contain an anchor, the pattern
308348
matches against the string as a whole, as in the following example:
@@ -311,12 +351,16 @@ matches against the string as a whole, as in the following example:
311351

312352
db.products.find( { description: { $regex: /S/ } } )
313353

314-
Then, the :query:`$regex` would match both documents:
354+
Example output:
315355

316356
.. code-block:: javascript
357+
:copyable: false
317358

318-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
319-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
359+
[
360+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
361+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
362+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
363+
]
320364

321365
.. _regex-dot-new-line:
322366

@@ -331,18 +375,24 @@ character (i.e. ``.``) to match all characters *including* new line as well as t
331375

332376
db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )
333377

334-
The query matches the following documents:
378+
Example output:
335379

336380
.. code-block:: javascript
381+
:copyable: false
337382

338-
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
339-
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
383+
[
384+
{ _id: 102, sku: 'xyz456', description: 'Many spaces before line' },
385+
{ _id: 103, sku: 'xyz789', description: 'Multiple\nline description' }
386+
]
340387

341-
*Without* the ``s`` option, the query would have matched only the following document:
388+
*Without* the ``s`` option, the example output is:
342389

343390
.. code-block:: javascript
391+
:copyable: false
344392

345-
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
393+
[
394+
{ _id: 102, sku: 'xyz456', description: 'Many spaces before line' }
395+
]
346396

347397
.. _regex-ignore-white-spaces:
348398

@@ -358,8 +408,37 @@ matching pattern:
358408
var pattern = "abc #category code\n123 #item number"
359409
db.products.find( { sku: { $regex: pattern, $options: "x" } } )
360410

361-
The query matches the following document:
411+
Example output:
412+
413+
.. code-block:: javascript
414+
:copyable: false
415+
416+
[
417+
{ _id: 100, sku: 'abc123', description: 'Single line description.' }
418+
]
419+
420+
.. _regex-match-case-in-strings:
421+
422+
Use a Regular Expression to Match Case in Strings
423+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
424+
425+
The following example uses the regular expression ``"(?i)a(?-i)bc"`` to
426+
match ``sku`` field strings that contain:
427+
428+
- ``"abc"``
429+
- ``"Abc"``
430+
431+
.. code-block:: javascript
432+
433+
db.products.find( { sku: { $regex: "(?i)a(?-i)bc" } } )
434+
435+
Example output:
362436

363437
.. code-block:: javascript
438+
:copyable: false
364439

365-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
440+
[
441+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
442+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
443+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
444+
]

0 commit comments

Comments
 (0)