Skip to content

Commit 0e3cd8d

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-24951 regex updates (BACKPORT) (#1805)
* DOCSP-24882 set window fields and transaction (#1734) * DOCSP-24882-setWindowFields-and-transaction * DOCSP-24882-setWindowFields-and-transaction * DOCSP-24882-setWindowFields-and-transaction * DOCSP-24882-setWindowFields-and-transaction * DOCSP-24882-setWindowFields-and-transaction * DOCSP-24882-setWindowFields-and-transaction * DOCSP-24882-setWindowFields-and-transaction * DOCSP-24882-setWindowFields-and-transaction * DOCSP-24882-setWindowFields-and-transaction * DOCSP-24882-setWindowFields-and-transaction Co-authored-by: jason-price-mongodb <[email protected]> * DOCSP-24882-setWindowFields-update * 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]> Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 4d71cc5 commit 0e3cd8d

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
@@ -101,13 +101,11 @@ Definition
101101

102102
- Requires ``$regex`` with ``$options`` syntax
103103

104-
105104
.. note::
106105

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

110-
111109
Behavior
112110
--------
113111

@@ -158,16 +156,32 @@ must use :query:`$options` for both:
158156
PCRE vs JavaScript
159157
``````````````````
160158

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

168182
.. code-block:: javascript
169183

170-
{ name: { $regex: '(?i)a(?-i)cme' } }
184+
{ name: { $regex: "(?i)a(?-i)cme" } }
171185

172186
``$regex`` and ``$not``
173187
```````````````````````
@@ -206,6 +220,7 @@ Index Use
206220
For case sensitive regular expression queries, if an index exists for
207221
the field, then MongoDB matches the regular expression against the
208222
values in the index, which can be faster than a collection scan.
223+
209224
Further optimization can occur if the regular expression is a "prefix
210225
expression", which means that all potential matches start with the same
211226
string. This allows MongoDB to construct a "range" from that prefix and
@@ -230,15 +245,17 @@ and is unable to utilize case-insensitive indexes.
230245
Examples
231246
--------
232247

233-
The following examples use a collection ``products`` with the following
234-
documents:
248+
The examples in this section use the following ``products`` collection:
235249

236250
.. code-block:: javascript
237251

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

243260
Perform a ``LIKE`` Match
244261
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -257,6 +274,17 @@ The example is analogous to the following SQL LIKE statement:
257274
SELECT * FROM products
258275
WHERE sku like "%789";
259276

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

262290
Perform Case-Insensitive Regular Expression Match
@@ -270,12 +298,16 @@ with ``ABC``.
270298

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

273-
The query matches the following documents:
301+
Example output:
274302

275303
.. code-block:: javascript
304+
:copyable: false
276305

277-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
278-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
306+
[
307+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
308+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
309+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
310+
]
279311

280312
.. _regex-multiline-match:
281313

@@ -289,18 +321,26 @@ with the letter ``S`` for multiline strings:
289321

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

292-
The query matches the following documents:
324+
Example output:
293325

294326
.. code-block:: javascript
327+
:copyable: false
295328

296-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
297-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
329+
[
330+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
331+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
332+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
333+
]
298334

299-
Without the ``m`` option, the query would match just the following document:
335+
Without the ``m`` option, the example output is:
300336

301337
.. code-block:: javascript
338+
:copyable: false
302339

303-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
340+
[
341+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
342+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
343+
]
304344

305345
If the :query:`$regex` pattern does not contain an anchor, the pattern
306346
matches against the string as a whole, as in the following example:
@@ -309,12 +349,16 @@ matches against the string as a whole, as in the following example:
309349

310350
db.products.find( { description: { $regex: /S/ } } )
311351

312-
Then, the :query:`$regex` would match both documents:
352+
Example output:
313353

314354
.. code-block:: javascript
355+
:copyable: false
315356

316-
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
317-
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
357+
[
358+
{ _id: 100, sku: 'abc123', description: 'Single line description.' },
359+
{ _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
360+
{ _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
361+
]
318362

319363
.. _regex-dot-new-line:
320364

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

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

332-
The query matches the following documents:
376+
Example output:
333377

334378
.. code-block:: javascript
379+
:copyable: false
335380

336-
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
337-
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
381+
[
382+
{ _id: 102, sku: 'xyz456', description: 'Many spaces before line' },
383+
{ _id: 103, sku: 'xyz789', description: 'Multiple\nline description' }
384+
]
338385

339-
*Without* the ``s`` option, the query would have matched only the following document:
386+
*Without* the ``s`` option, the example output is:
340387

341388
.. code-block:: javascript
389+
:copyable: false
342390

343-
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
391+
[
392+
{ _id: 102, sku: 'xyz456', description: 'Many spaces before line' }
393+
]
344394

345395
.. _regex-ignore-white-spaces:
346396

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

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

361435
.. code-block:: javascript
436+
:copyable: false
362437

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

0 commit comments

Comments
 (0)