@@ -103,13 +103,11 @@ Definition
103
103
104
104
- Requires ``$regex`` with ``$options`` syntax
105
105
106
-
107
106
.. note::
108
107
109
108
The :query:`$regex` operator does not support the global search
110
109
modifier ``g``.
111
110
112
-
113
111
Behavior
114
112
--------
115
113
@@ -160,16 +158,32 @@ must use :query:`$options` for both:
160
158
PCRE vs JavaScript
161
159
``````````````````
162
160
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"``:
169
183
170
184
.. code-block:: javascript
171
185
172
- { name: { $regex: ' (?i)a(?-i)cme' } }
186
+ { name: { $regex: " (?i)a(?-i)cme" } }
173
187
174
188
``$regex`` and ``$not``
175
189
```````````````````````
@@ -208,6 +222,7 @@ Index Use
208
222
For case sensitive regular expression queries, if an index exists for
209
223
the field, then MongoDB matches the regular expression against the
210
224
values in the index, which can be faster than a collection scan.
225
+
211
226
Further optimization can occur if the regular expression is a "prefix
212
227
expression", which means that all potential matches start with the same
213
228
string. This allows MongoDB to construct a "range" from that prefix and
@@ -232,15 +247,17 @@ and is unable to utilize case-insensitive indexes.
232
247
Examples
233
248
--------
234
249
235
- The following examples use a collection ``products`` with the following
236
- documents:
250
+ The examples in this section use the following ``products`` collection:
237
251
238
252
.. code-block:: javascript
239
253
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
+ ] )
244
261
245
262
Perform a ``LIKE`` Match
246
263
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -259,6 +276,17 @@ The example is analogous to the following SQL LIKE statement:
259
276
SELECT * FROM products
260
277
WHERE sku like "%789";
261
278
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
+
262
290
.. _regex-case-insensitive:
263
291
264
292
Perform Case-Insensitive Regular Expression Match
@@ -272,12 +300,16 @@ with ``ABC``.
272
300
273
301
db.products.find( { sku: { $regex: /^ABC/i } } )
274
302
275
- The query matches the following documents :
303
+ Example output :
276
304
277
305
.. code-block:: javascript
306
+ :copyable: false
278
307
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
+ ]
281
313
282
314
.. _regex-multiline-match:
283
315
@@ -291,18 +323,26 @@ with the letter ``S`` for multiline strings:
291
323
292
324
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
293
325
294
- The query matches the following documents :
326
+ Example output :
295
327
296
328
.. code-block:: javascript
329
+ :copyable: false
297
330
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
+ ]
300
336
301
- Without the ``m`` option, the query would match just the following document :
337
+ Without the ``m`` option, the example output is :
302
338
303
339
.. code-block:: javascript
340
+ :copyable: false
304
341
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
+ ]
306
346
307
347
If the :query:`$regex` pattern does not contain an anchor, the pattern
308
348
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:
311
351
312
352
db.products.find( { description: { $regex: /S/ } } )
313
353
314
- Then, the :query:`$regex` would match both documents :
354
+ Example output :
315
355
316
356
.. code-block:: javascript
357
+ :copyable: false
317
358
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
+ ]
320
364
321
365
.. _regex-dot-new-line:
322
366
@@ -331,18 +375,24 @@ character (i.e. ``.``) to match all characters *including* new line as well as t
331
375
332
376
db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )
333
377
334
- The query matches the following documents :
378
+ Example output :
335
379
336
380
.. code-block:: javascript
381
+ :copyable: false
337
382
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
+ ]
340
387
341
- *Without* the ``s`` option, the query would have matched only the following document :
388
+ *Without* the ``s`` option, the example output is :
342
389
343
390
.. code-block:: javascript
391
+ :copyable: false
344
392
345
- { "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
393
+ [
394
+ { _id: 102, sku: 'xyz456', description: 'Many spaces before line' }
395
+ ]
346
396
347
397
.. _regex-ignore-white-spaces:
348
398
@@ -358,8 +408,37 @@ matching pattern:
358
408
var pattern = "abc #category code\n123 #item number"
359
409
db.products.find( { sku: { $regex: pattern, $options: "x" } } )
360
410
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:
362
436
363
437
.. code-block:: javascript
438
+ :copyable: false
364
439
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