Skip to content

Commit 0ad591a

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-24951 regex updates (BACKPORT) (#1808)
* 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]> * DOCSP-24951 regex updates Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 406dc9c commit 0ad591a

File tree

1 file changed

+112
-31
lines changed

1 file changed

+112
-31
lines changed

source/reference/operator/query/regex.txt

Lines changed: 112 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,32 @@ must use :query:`$options` for both:
151151
PCRE vs JavaScript
152152
``````````````````
153153

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

161177
.. code-block:: javascript
162178

163-
{ name: { $regex: '(?i)a(?-i)cme' } }
179+
{ name: { $regex: "(?i)a(?-i)cme" } }
164180

165181
``$regex`` and ``$not``
166182
```````````````````````
@@ -199,6 +215,7 @@ Index Use
199215
For case sensitive regular expression queries, if an index exists for
200216
the field, then MongoDB matches the regular expression against the
201217
values in the index, which can be faster than a collection scan.
218+
202219
Further optimization can occur if the regular expression is a "prefix
203220
expression", which means that all potential matches start with the same
204221
string. This allows MongoDB to construct a "range" from that prefix and
@@ -223,15 +240,17 @@ and is unable to utilize case-insensitive indexes.
223240
Examples
224241
--------
225242

226-
The following examples use a collection ``products`` with the following
227-
documents:
243+
The examples in this section use the following ``products`` collection:
228244

229245
.. code-block:: javascript
230246

231-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
232-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
233-
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
234-
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
247+
db.products.insertMany( [
248+
{ _id: 100, sku: "abc123", description: "Single line description." },
249+
{ _id: 101, sku: "abc789", description: "First line\nSecond line" },
250+
{ _id: 102, sku: "xyz456", description: "Many spaces before line" },
251+
{ _id: 103, sku: "xyz789", description: "Multiple\nline description" },
252+
{ _id: 104, sku: "Abc789", description: "SKU starts with A" }
253+
] )
235254

236255
Perform a ``LIKE`` Match
237256
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -250,6 +269,17 @@ The example is analogous to the following SQL LIKE statement:
250269
SELECT * FROM products
251270
WHERE sku like "%789";
252271

272+
Example output:
273+
274+
.. code-block:: javascript
275+
:copyable: false
276+
277+
[
278+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
279+
{ _id: 103, sku: 'xyz789', description: 'Multiple\nline description' },
280+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
281+
]
282+
253283
.. _regex-case-insensitive:
254284

255285
Perform Case-Insensitive Regular Expression Match
@@ -263,12 +293,16 @@ with ``ABC``.
263293

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

266-
The query matches the following documents:
296+
Example output:
267297

268298
.. code-block:: javascript
299+
:copyable: false
269300

270-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
271-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
301+
[
302+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
303+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
304+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
305+
]
272306

273307
.. _regex-multiline-match:
274308

@@ -282,18 +316,26 @@ with the letter ``S`` for multiline strings:
282316

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

285-
The query matches the following documents:
319+
Example output:
286320

287321
.. code-block:: javascript
322+
:copyable: false
288323

289-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
290-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
324+
[
325+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
326+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
327+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
328+
]
291329

292-
Without the ``m`` option, the query would match just the following document:
330+
Without the ``m`` option, the example output is:
293331

294332
.. code-block:: javascript
333+
:copyable: false
295334

296-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
335+
[
336+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
337+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
338+
]
297339

298340
If the :query:`$regex` pattern does not contain an anchor, the pattern
299341
matches against the string as a whole, as in the following example:
@@ -302,12 +344,16 @@ matches against the string as a whole, as in the following example:
302344

303345
db.products.find( { description: { $regex: /S/ } } )
304346

305-
Then, the :query:`$regex` would match both documents:
347+
Example output:
306348

307349
.. code-block:: javascript
350+
:copyable: false
308351

309-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
310-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
352+
[
353+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
354+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
355+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
356+
]
311357

312358
.. _regex-dot-new-line:
313359

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

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

325-
The query matches the following documents:
371+
Example output:
326372

327373
.. code-block:: javascript
374+
:copyable: false
328375

329-
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
330-
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
376+
[
377+
{ _id: 102, sku: 'xyz456', description: 'Many spaces before line' },
378+
{ _id: 103, sku: 'xyz789', description: 'Multiple\nline description' }
379+
]
331380

332-
*Without* the ``s`` option, the query would have matched only the following document:
381+
*Without* the ``s`` option, the example output is:
333382

334383
.. code-block:: javascript
384+
:copyable: false
335385

336-
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
386+
[
387+
{ _id: 102, sku: 'xyz456', description: 'Many spaces before line' }
388+
]
337389

338390
.. _regex-ignore-white-spaces:
339391

@@ -349,8 +401,37 @@ matching pattern:
349401
var pattern = "abc #category code\n123 #item number"
350402
db.products.find( { sku: { $regex: pattern, $options: "x" } } )
351403

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

354430
.. code-block:: javascript
431+
:copyable: false
355432

356-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
433+
[
434+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
435+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
436+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
437+
]

0 commit comments

Comments
 (0)