Skip to content

Commit bb7d5d5

Browse files
committed
PHPLIB-243: Add regex query examples to CRUD tutorial
1 parent c346c34 commit bb7d5d5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/tutorial/crud.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,48 @@ The output would then resemble::
331331
10025: NEW YORK, NY
332332
90201: BELL GARDENS, CA
333333

334+
Regular Expressions
335+
~~~~~~~~~~~~~~~~~~~
336+
337+
Filter criteria may include regular expressions, either by using the
338+
:php:`MongoDB\\BSON\\Regex <mongodb-bson-regex>` class directory or the
339+
:query:`$regex` operator.
340+
341+
The following example lists documents in the ``zips`` collection where the city
342+
name starts with "garden" and the state is Texas:
343+
344+
.. code-block:: php
345+
346+
<?php
347+
348+
$collection = (new MongoDB\Client)->demo->zips;
349+
350+
$cursor = $collection->find([
351+
'city' => new MongoDB\BSON\Regex('^garden', 'i'),
352+
'state' => 'TX',
353+
]);
354+
355+
foreach ($cursor as $document) {
356+
printf("%s: %s, %s\n", $document['_id'], $document['city'], $document['state']);
357+
}
358+
359+
The output would then resemble::
360+
361+
78266: GARDEN RIDGE, TX
362+
79739: GARDEN CITY, TX
363+
79758: GARDENDALE, TX
364+
365+
An equivalent filter could be constructed using the :query:`$regex` operator:
366+
367+
.. code-block:: php
368+
369+
[
370+
'city' => ['$regex' => '^garden', '$options' => 'i'],
371+
'state' => 'TX',
372+
]
373+
374+
.. seealso:: :manual:`$regex </reference/operator/query/regex>` in the MongoDB manual
375+
334376
Complex Queries with Aggregation
335377
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
336378

0 commit comments

Comments
 (0)