@@ -151,16 +151,32 @@ must use :query:`$options` for both:
151
151
PCRE vs JavaScript
152
152
``````````````````
153
153
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"``:
160
176
161
177
.. code-block:: javascript
162
178
163
- { name: { $regex: ' (?i)a(?-i)cme' } }
179
+ { name: { $regex: " (?i)a(?-i)cme" } }
164
180
165
181
``$regex`` and ``$not``
166
182
```````````````````````
@@ -199,6 +215,7 @@ Index Use
199
215
For case sensitive regular expression queries, if an index exists for
200
216
the field, then MongoDB matches the regular expression against the
201
217
values in the index, which can be faster than a collection scan.
218
+
202
219
Further optimization can occur if the regular expression is a "prefix
203
220
expression", which means that all potential matches start with the same
204
221
string. This allows MongoDB to construct a "range" from that prefix and
@@ -223,15 +240,17 @@ and is unable to utilize case-insensitive indexes.
223
240
Examples
224
241
--------
225
242
226
- The following examples use a collection ``products`` with the following
227
- documents:
243
+ The examples in this section use the following ``products`` collection:
228
244
229
245
.. code-block:: javascript
230
246
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
+ ] )
235
254
236
255
Perform a ``LIKE`` Match
237
256
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -250,6 +269,17 @@ The example is analogous to the following SQL LIKE statement:
250
269
SELECT * FROM products
251
270
WHERE sku like "%789";
252
271
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
+
253
283
.. _regex-case-insensitive:
254
284
255
285
Perform Case-Insensitive Regular Expression Match
@@ -263,12 +293,16 @@ with ``ABC``.
263
293
264
294
db.products.find( { sku: { $regex: /^ABC/i } } )
265
295
266
- The query matches the following documents :
296
+ Example output :
267
297
268
298
.. code-block:: javascript
299
+ :copyable: false
269
300
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
+ ]
272
306
273
307
.. _regex-multiline-match:
274
308
@@ -282,18 +316,26 @@ with the letter ``S`` for multiline strings:
282
316
283
317
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
284
318
285
- The query matches the following documents :
319
+ Example output :
286
320
287
321
.. code-block:: javascript
322
+ :copyable: false
288
323
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
+ ]
291
329
292
- Without the ``m`` option, the query would match just the following document :
330
+ Without the ``m`` option, the example output is :
293
331
294
332
.. code-block:: javascript
333
+ :copyable: false
295
334
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
+ ]
297
339
298
340
If the :query:`$regex` pattern does not contain an anchor, the pattern
299
341
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:
302
344
303
345
db.products.find( { description: { $regex: /S/ } } )
304
346
305
- Then, the :query:`$regex` would match both documents :
347
+ Example output :
306
348
307
349
.. code-block:: javascript
350
+ :copyable: false
308
351
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
+ ]
311
357
312
358
.. _regex-dot-new-line:
313
359
@@ -322,18 +368,24 @@ character (i.e. ``.``) to match all characters *including* new line as well as t
322
368
323
369
db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )
324
370
325
- The query matches the following documents :
371
+ Example output :
326
372
327
373
.. code-block:: javascript
374
+ :copyable: false
328
375
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
+ ]
331
380
332
- *Without* the ``s`` option, the query would have matched only the following document :
381
+ *Without* the ``s`` option, the example output is :
333
382
334
383
.. code-block:: javascript
384
+ :copyable: false
335
385
336
- { "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
386
+ [
387
+ { _id: 102, sku: 'xyz456', description: 'Many spaces before line' }
388
+ ]
337
389
338
390
.. _regex-ignore-white-spaces:
339
391
@@ -349,8 +401,37 @@ matching pattern:
349
401
var pattern = "abc #category code\n123 #item number"
350
402
db.products.find( { sku: { $regex: pattern, $options: "x" } } )
351
403
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:
353
429
354
430
.. code-block:: javascript
431
+ :copyable: false
355
432
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