Skip to content

Commit 2f7bf84

Browse files
Merge 4.4 into 4.5 (#3032)
2 parents ffacc6b + 0f697e1 commit 2f7bf84

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

docs/fundamentals/read-operations.txt

+59-8
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,13 @@ You can use Laravel's Eloquent object-relational mapper (ORM) to create models
5757
that represent MongoDB collections and chain methods on them to specify
5858
query criteria.
5959

60-
To retrieve documents that match a set of criteria, pass a query filter to the
61-
``where()`` method.
60+
To retrieve documents that match a set of criteria, call the ``where()``
61+
method on the collection's corresponding Eloquent model, then pass a query
62+
filter to the method.
6263

6364
A query filter specifies field value requirements and instructs the find
6465
operation to return only documents that meet these requirements.
6566

66-
You can use Laravel's Eloquent object-relational mapper (ORM) to create models
67-
that represent MongoDB collections. To retrieve documents from a collection,
68-
call the ``where()`` method on the collection's corresponding Eloquent model.
69-
7067
You can use one of the following ``where()`` method calls to build a query:
7168

7269
- ``where('<field name>', <value>)`` builds a query that matches documents in
@@ -79,7 +76,7 @@ You can use one of the following ``where()`` method calls to build a query:
7976
To apply multiple sets of criteria to the find operation, you can chain a series
8077
of ``where()`` methods together.
8178

82-
After building your query with the ``where()`` method, chain the ``get()``
79+
After building your query by using the ``where()`` method, chain the ``get()``
8380
method to retrieve the query results.
8481

8582
This example calls two ``where()`` methods on the ``Movie`` Eloquent model to
@@ -150,6 +147,60 @@ retrieve documents that meet the following criteria:
150147
To learn how to query by using the Laravel query builder instead of the
151148
Eloquent ORM, see the :ref:`laravel-query-builder` page.
152149

150+
Match Array Field Elements
151+
~~~~~~~~~~~~~~~~~~~~~~~~~~
152+
153+
You can specify a query filter to match array field elements when
154+
retrieving documents. If your documents contain an array field, you can
155+
match documents based on if the value contains all or some specified
156+
array elements.
157+
158+
You can use one of the following ``where()`` method calls to build a
159+
query on an array field:
160+
161+
- ``where('<array field>', <array>)`` builds a query that matches documents in
162+
which the array field value is exactly the specified array
163+
164+
- ``where('<array field>', 'in', <array>)`` builds a query
165+
that matches documents in which the array field value contains one or
166+
more of the specified array elements
167+
168+
After building your query by using the ``where()`` method, chain the ``get()``
169+
method to retrieve the query results.
170+
171+
Select from the following :guilabel:`Exact Array Match` and
172+
:guilabel:`Element Match` tabs to view the query syntax for each pattern:
173+
174+
.. tabs::
175+
176+
.. tab:: Exact Array Match
177+
:tabid: exact-array
178+
179+
This example retrieves documents in which the ``countries`` array is
180+
exactly ``['Indonesia', 'Canada']``:
181+
182+
.. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
183+
:language: php
184+
:dedent:
185+
:start-after: start-exact-array
186+
:end-before: end-exact-array
187+
188+
.. tab:: Element Match
189+
:tabid: element-match
190+
191+
This example retrieves documents in which the ``countries`` array
192+
contains one of the values in the array ``['Canada', 'Egypt']``:
193+
194+
.. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
195+
:language: php
196+
:dedent:
197+
:start-after: start-elem-match
198+
:end-before: end-elem-match
199+
200+
To learn how to query array fields by using the Laravel query builder instead of the
201+
Eloquent ORM, see the :ref:`laravel-query-builder-elemMatch` section in
202+
the Query Builder guide.
203+
153204
.. _laravel-retrieve-all:
154205

155206
Retrieve All Documents in a Collection
@@ -200,7 +251,7 @@ by the ``$search`` field in your query filter that you pass to the
200251
``where()`` method. The ``$text`` operator performs a text search on the
201252
text-indexed fields. The ``$search`` field specifies the text to search for.
202253

203-
After building your query with the ``where()`` method, chain the ``get()``
254+
After building your query by using the ``where()`` method, chain the ``get()``
204255
method to retrieve the query results.
205256

206257
This example calls the ``where()`` method on the ``Movie`` Eloquent model to

docs/includes/fundamentals/read-operations/ReadOperationsTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,35 @@ public function testTextRelevance(): void
133133
$this->assertCount(1, $movies);
134134
$this->assertEquals('this is a love story', $movies[0]->plot);
135135
}
136+
137+
/**
138+
* @runInSeparateProcess
139+
* @preserveGlobalState disabled
140+
*/
141+
public function exactArrayMatch(): void
142+
{
143+
// start-exact-array
144+
$movies = Movie::where('countries', ['Indonesia', 'Canada'])
145+
->get();
146+
// end-exact-array
147+
148+
$this->assertNotNull($movies);
149+
$this->assertCount(1, $movies);
150+
$this->assertEquals('Title 1', $movies[0]->title);
151+
}
152+
153+
/**
154+
* @runInSeparateProcess
155+
* @preserveGlobalState disabled
156+
*/
157+
public function arrayElemMatch(): void
158+
{
159+
// start-elem-match
160+
$movies = Movie::where('countries', 'in', ['Canada', 'Egypt'])
161+
->get();
162+
// end-elem-match
163+
164+
$this->assertNotNull($movies);
165+
$this->assertCount(2, $movies);
166+
}
136167
}

0 commit comments

Comments
 (0)