@@ -102,13 +102,11 @@ Definition
102102
103103 - Requires ``$regex`` with ``$options`` syntax
104104
105-
106105 .. note::
107106
108107 The :query:`$regex` operator does not support the global search
109108 modifier ``g``.
110109
111-
112110Behavior
113111--------
114112
@@ -159,22 +157,34 @@ must use :query:`$options` for both:
159157PCRE Versus JavaScript
160158``````````````````````
161159
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.
165163
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:
170165
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"``:
172182
173- { name: { $regex: '(?i)m(?-i)ongoDB' } }
183+ .. code-block:: javascript
174184
175- .. note:: PCRE Library
185+ { name: { $regex: "(?i)a(?-i)cme" } }
176186
177- .. include:: /includes/fact-6.1-pcre2.rst
187+ .. include:: /includes/fact-6.1-pcre2.rst
178188
179189``$regex`` and ``$not``
180190```````````````````````
@@ -209,6 +219,7 @@ Index Use
209219For case sensitive regular expression queries, if an index exists for
210220the field, then MongoDB matches the regular expression against the
211221values in the index, which can be faster than a collection scan.
222+
212223Further optimization can occur if the regular expression is a "prefix
213224expression", which means that all potential matches start with the same
214225string. This allows MongoDB to construct a "range" from that prefix and
@@ -233,15 +244,17 @@ and is unable to utilize case-insensitive indexes.
233244Examples
234245--------
235246
236- The following examples use a collection ``products`` with the following
237- documents:
247+ The examples in this section use the following ``products`` collection:
238248
239249.. code-block:: javascript
240250
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+ ] )
245258
246259Perform a ``LIKE`` Match
247260~~~~~~~~~~~~~~~~~~~~~~~~
@@ -260,6 +273,17 @@ The example is analogous to the following SQL LIKE statement:
260273 SELECT * FROM products
261274 WHERE sku like "%789";
262275
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+
263287.. _regex-case-insensitive:
264288
265289Perform Case-Insensitive Regular Expression Match
@@ -273,12 +297,16 @@ with ``ABC``.
273297
274298 db.products.find( { sku: { $regex: /^ABC/i } } )
275299
276- The query matches the following documents :
300+ Example output :
277301
278302.. code-block:: javascript
303+ :copyable: false
279304
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+ ]
282310
283311.. _regex-multiline-match:
284312
@@ -292,18 +320,26 @@ with the letter ``S`` for multiline strings:
292320
293321 db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
294322
295- The query matches the following documents :
323+ Example output :
296324
297325.. code-block:: javascript
326+ :copyable: false
298327
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+ ]
301333
302- Without the ``m`` option, the query would match just the following document :
334+ Without the ``m`` option, the example output is :
303335
304336.. code-block:: javascript
337+ :copyable: false
305338
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+ ]
307343
308344If the :query:`$regex` pattern does not contain an anchor, the pattern
309345matches 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:
312348
313349 db.products.find( { description: { $regex: /S/ } } )
314350
315- Then, the :query:`$regex` would match both documents :
351+ Example output :
316352
317353.. code-block:: javascript
354+ :copyable: false
318355
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+ ]
321361
322362.. _regex-dot-new-line:
323363
@@ -332,18 +372,24 @@ character (i.e. ``.``) to match all characters *including* new line as well as t
332372
333373 db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )
334374
335- The query matches the following documents :
375+ Example output :
336376
337377.. code-block:: javascript
378+ :copyable: false
338379
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+ ]
341384
342- *Without* the ``s`` option, the query would have matched only the following document :
385+ *Without* the ``s`` option, the example output is :
343386
344387.. code-block:: javascript
388+ :copyable: false
345389
346- { "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
390+ [
391+ { _id: 102, sku: 'xyz456', description: 'Many spaces before line' }
392+ ]
347393
348394.. _regex-ignore-white-spaces:
349395
@@ -359,11 +405,40 @@ matching pattern:
359405 var pattern = "abc #category code\n123 #item number"
360406 db.products.find( { sku: { $regex: pattern, $options: "x" } } )
361407
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"``
363427
364428.. code-block:: javascript
365429
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+ ]
367442
368443.. _regex-example-pcre2-ucp:
369444
0 commit comments