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