Skip to content

Commit 67f3b4f

Browse files
committed
DOCSP-38380: array reads
1 parent 0a143cc commit 67f3b4f

File tree

2 files changed

+89
-6
lines changed

2 files changed

+89
-6
lines changed

docs/fundamentals/read-operations.txt

Lines changed: 57 additions & 6 deletions
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 modelm then pass a query
62+
filter.
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
@@ -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 with 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

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ protected function setUp(): void
3333
['title' => 'movie_a', 'plot' => 'this is a love story'],
3434
['title' => 'movie_b', 'plot' => 'love is a long story'],
3535
['title' => 'movie_c', 'plot' => 'went on a trip'],
36+
['title' => 'movie_c', 'plot' => 'went on a trip'],
3637
]);
3738
}
3839

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

0 commit comments

Comments
 (0)