@@ -102,13 +102,11 @@ Definition
102
102
103
103
- Requires ``$regex`` with ``$options`` syntax
104
104
105
-
106
105
.. note::
107
106
108
107
The :query:`$regex` operator does not support the global search
109
108
modifier ``g``.
110
109
111
-
112
110
Behavior
113
111
--------
114
112
@@ -159,22 +157,34 @@ must use :query:`$options` for both:
159
157
PCRE Versus JavaScript
160
158
``````````````````````
161
159
162
- To use {+pcre-abbr+}-supported features in the regex pattern that are
163
- unsupported in JavaScript, you must use the :query:`$regex` operator and
164
- include the features in the pattern string.
160
+ To use {+pcre-abbr+}-supported features in a regular expression that
161
+ aren't supported in JavaScript, you must use the :query:`$regex`
162
+ operator and specify the regular expression as a string.
165
163
166
- For example, to turn on the case-insensitivity option (``i``), place
167
- ``(?i)`` anywhere in a pattern. ``(?-i)`` turns this option off. The
168
- following example matches ``mongoDB`` and ``MongoDB``, but not
169
- ``mongodb``:
164
+ To match case-insensitive strings:
170
165
171
- .. code-block:: javascript
166
+ - ``"(?i)"`` begins a case-insensitive match.
167
+ - ``"(?-i)"`` ends a case-insensitive match.
168
+
169
+ For example, the regular expression ``"(?i)a(?-i)cme"`` matches strings
170
+ that:
171
+
172
+ - Begin with ``"a"`` or ``"A"``. This is a case-insensitive match.
173
+ - End with ``"cme"``. This is a case-sensitive match.
174
+
175
+ These strings match the example regular expression:
176
+
177
+ - ``"acme"``
178
+ - ``"Acme"``
179
+
180
+ The following example uses the :query:`$regex` operator to find ``name``
181
+ field strings that match the regular expression ``"(?i)a(?-i)cme"``:
172
182
173
- { name: { $regex: '(?i)m(?-i)ongoDB' } }
183
+ .. code-block:: javascript
174
184
175
- .. note:: PCRE Library
185
+ { name: { $regex: "(?i)a(?-i)cme" } }
176
186
177
- .. include:: /includes/fact-6.1-pcre2.rst
187
+ .. include:: /includes/fact-6.1-pcre2.rst
178
188
179
189
``$regex`` and ``$not``
180
190
```````````````````````
@@ -209,6 +219,7 @@ Index Use
209
219
For case sensitive regular expression queries, if an index exists for
210
220
the field, then MongoDB matches the regular expression against the
211
221
values in the index, which can be faster than a collection scan.
222
+
212
223
Further optimization can occur if the regular expression is a "prefix
213
224
expression", which means that all potential matches start with the same
214
225
string. This allows MongoDB to construct a "range" from that prefix and
@@ -233,15 +244,17 @@ and is unable to utilize case-insensitive indexes.
233
244
Examples
234
245
--------
235
246
236
- The following examples use a collection ``products`` with the following
237
- documents:
247
+ The examples in this section use the following ``products`` collection:
238
248
239
249
.. code-block:: javascript
240
250
241
- { "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
242
- { "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
243
- { "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
244
- { "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
251
+ db.products.insertMany( [
252
+ { _id: 100, sku: "abc123", description: "Single line description." },
253
+ { _id: 101, sku: "abc789", description: "First line\nSecond line" },
254
+ { _id: 102, sku: "xyz456", description: "Many spaces before line" },
255
+ { _id: 103, sku: "xyz789", description: "Multiple\nline description" },
256
+ { _id: 104, sku: "Abc789", description: "SKU starts with A" }
257
+ ] )
245
258
246
259
Perform a ``LIKE`` Match
247
260
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -260,6 +273,17 @@ The example is analogous to the following SQL LIKE statement:
260
273
SELECT * FROM products
261
274
WHERE sku like "%789";
262
275
276
+ Example output:
277
+
278
+ .. code-block:: javascript
279
+ :copyable: false
280
+
281
+ [
282
+ { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
283
+ { _id: 103, sku: 'xyz789', description: 'Multiple\nline description' },
284
+ { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
285
+ ]
286
+
263
287
.. _regex-case-insensitive:
264
288
265
289
Perform Case-Insensitive Regular Expression Match
@@ -273,12 +297,16 @@ with ``ABC``.
273
297
274
298
db.products.find( { sku: { $regex: /^ABC/i } } )
275
299
276
- The query matches the following documents :
300
+ Example output :
277
301
278
302
.. code-block:: javascript
303
+ :copyable: false
279
304
280
- { "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
281
- { "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
305
+ [
306
+ { _id: 100, sku: 'abc123', description: 'Single line description.' },
307
+ { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
308
+ { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
309
+ ]
282
310
283
311
.. _regex-multiline-match:
284
312
@@ -292,18 +320,26 @@ with the letter ``S`` for multiline strings:
292
320
293
321
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
294
322
295
- The query matches the following documents :
323
+ Example output :
296
324
297
325
.. code-block:: javascript
326
+ :copyable: false
298
327
299
- { "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
300
- { "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
328
+ [
329
+ { _id: 100, sku: 'abc123', description: 'Single line description.' },
330
+ { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
331
+ { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
332
+ ]
301
333
302
- Without the ``m`` option, the query would match just the following document :
334
+ Without the ``m`` option, the example output is :
303
335
304
336
.. code-block:: javascript
337
+ :copyable: false
305
338
306
- { "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
339
+ [
340
+ { _id: 100, sku: 'abc123', description: 'Single line description.' },
341
+ { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
342
+ ]
307
343
308
344
If the :query:`$regex` pattern does not contain an anchor, the pattern
309
345
matches against the string as a whole, as in the following example:
@@ -312,12 +348,16 @@ matches against the string as a whole, as in the following example:
312
348
313
349
db.products.find( { description: { $regex: /S/ } } )
314
350
315
- Then, the :query:`$regex` would match both documents :
351
+ Example output :
316
352
317
353
.. code-block:: javascript
354
+ :copyable: false
318
355
319
- { "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
320
- { "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
356
+ [
357
+ { _id: 100, sku: 'abc123', description: 'Single line description.' },
358
+ { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
359
+ { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
360
+ ]
321
361
322
362
.. _regex-dot-new-line:
323
363
@@ -332,18 +372,24 @@ character (i.e. ``.``) to match all characters *including* new line as well as t
332
372
333
373
db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )
334
374
335
- The query matches the following documents :
375
+ Example output :
336
376
337
377
.. code-block:: javascript
378
+ :copyable: false
338
379
339
- { "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
340
- { "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
380
+ [
381
+ { _id: 102, sku: 'xyz456', description: 'Many spaces before line' },
382
+ { _id: 103, sku: 'xyz789', description: 'Multiple\nline description' }
383
+ ]
341
384
342
- *Without* the ``s`` option, the query would have matched only the following document :
385
+ *Without* the ``s`` option, the example output is :
343
386
344
387
.. code-block:: javascript
388
+ :copyable: false
345
389
346
- { "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
390
+ [
391
+ { _id: 102, sku: 'xyz456', description: 'Many spaces before line' }
392
+ ]
347
393
348
394
.. _regex-ignore-white-spaces:
349
395
@@ -359,11 +405,40 @@ matching pattern:
359
405
var pattern = "abc #category code\n123 #item number"
360
406
db.products.find( { sku: { $regex: pattern, $options: "x" } } )
361
407
362
- The query matches the following document:
408
+ Example output:
409
+
410
+ .. code-block:: javascript
411
+ :copyable: false
412
+
413
+ [
414
+ { _id: 100, sku: 'abc123', description: 'Single line description.' }
415
+ ]
416
+
417
+ .. _regex-match-case-in-strings:
418
+
419
+ Use a Regular Expression to Match Case in Strings
420
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
421
+
422
+ The following example uses the regular expression ``"(?i)a(?-i)bc"`` to
423
+ match ``sku`` field strings that contain:
424
+
425
+ - ``"abc"``
426
+ - ``"Abc"``
363
427
364
428
.. code-block:: javascript
365
429
366
- { "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
430
+ db.products.find( { sku: { $regex: "(?i)a(?-i)bc" } } )
431
+
432
+ Example output:
433
+
434
+ .. code-block:: javascript
435
+ :copyable: false
436
+
437
+ [
438
+ { _id: 100, sku: 'abc123', description: 'Single line description.' },
439
+ { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },
440
+ { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }
441
+ ]
367
442
368
443
.. _regex-example-pcre2-ucp:
369
444
0 commit comments