@@ -57,16 +57,13 @@ You can use Laravel's Eloquent object-relational mapper (ORM) to create models
57
57
that represent MongoDB collections and chain methods on them to specify
58
58
query criteria.
59
59
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.
62
63
63
64
A query filter specifies field value requirements and instructs the find
64
65
operation to return only documents that meet these requirements.
65
66
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
-
70
67
You can use one of the following ``where()`` method calls to build a query:
71
68
72
69
- ``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:
79
76
To apply multiple sets of criteria to the find operation, you can chain a series
80
77
of ``where()`` methods together.
81
78
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()``
83
80
method to retrieve the query results.
84
81
85
82
This example calls two ``where()`` methods on the ``Movie`` Eloquent model to
@@ -150,6 +147,60 @@ retrieve documents that meet the following criteria:
150
147
To learn how to query by using the Laravel query builder instead of the
151
148
Eloquent ORM, see the :ref:`laravel-query-builder` page.
152
149
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
+
153
204
.. _laravel-retrieve-all:
154
205
155
206
Retrieve All Documents in a Collection
@@ -200,7 +251,7 @@ by the ``$search`` field in your query filter that you pass to the
200
251
``where()`` method. The ``$text`` operator performs a text search on the
201
252
text-indexed fields. The ``$search`` field specifies the text to search for.
202
253
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()``
204
255
method to retrieve the query results.
205
256
206
257
This example calls the ``where()`` method on the ``Movie`` Eloquent model to
0 commit comments