@@ -13,7 +13,7 @@ Query Builder
13
13
14
14
The database driver plugs right into the original query builder.
15
15
16
- When using MongoDB connections, you will be able to build fluent queries to
16
+ When using MongoDB connections, you will be able to build fluent queries to
17
17
perform database operations.
18
18
19
19
For your convenience, there is a ``collection`` alias for ``table`` and
@@ -85,7 +85,7 @@ Available operations
85
85
86
86
$users = User::whereIn('age', [16, 18, 20])->get();
87
87
88
- When using ``whereNotIn`` objects will be returned if the field is
88
+ When using ``whereNotIn`` objects will be returned if the field is
89
89
non-existent. Combine with ``whereNotNull('age')`` to omit those documents.
90
90
91
91
**whereBetween**
@@ -137,7 +137,7 @@ The usage is the same as ``whereMonth`` / ``whereDay`` / ``whereYear`` / ``where
137
137
138
138
**groupBy**
139
139
140
- Selected columns that are not grouped will be aggregated with the ``$last``
140
+ Selected columns that are not grouped will be aggregated with the ``$last``
141
141
function.
142
142
143
143
.. code-block:: php
@@ -230,7 +230,7 @@ You may also specify more columns to update:
230
230
MongoDB-specific operators
231
231
~~~~~~~~~~~~~~~~~~~~~~~~~~
232
232
233
- In addition to the Laravel Eloquent operators, all available MongoDB query
233
+ In addition to the Laravel Eloquent operators, all available MongoDB query
234
234
operators can be used with ``where``:
235
235
236
236
.. code-block:: php
@@ -279,7 +279,7 @@ Selects documents where values match a specified regular expression.
279
279
280
280
.. note::
281
281
282
- You can also use the Laravel regexp operations. These will automatically
282
+ You can also use the Laravel regexp operations. These will automatically
283
283
convert your regular expression string to a ``MongoDB\BSON\Regex`` object.
284
284
285
285
.. code-block:: php
@@ -292,9 +292,37 @@ The inverse of regexp:
292
292
293
293
User::where('name', 'not regexp', '/.*doe/i')->get();
294
294
295
+ **ElemMatch**
296
+
297
+ The :manual:`$elemMatch </reference/operator/query/elemMatch//>` operator
298
+ matches documents that contain an array field with at least one element that
299
+ matches all the specified query criteria.
300
+
301
+ The following query matches only those documents where the results array
302
+ contains at least one element that is both greater than or equal to 80 and
303
+ is less than 85:
304
+
305
+ .. code-block:: php
306
+
307
+ User::where('results', 'elemMatch', ['gte' => 80, 'lt' => 85])->get();
308
+
309
+ A closure can be used to create more complex sub-queries.
310
+
311
+ The following query matches only those documents where the results array
312
+ contains at least one element with both product equal to "xyz" and score
313
+ greater than or equal to 8:
314
+
315
+ .. code-block:: php
316
+
317
+ User::where('results', 'elemMatch', function (Builder $builder) {
318
+ $builder
319
+ ->where('product', 'xyz')
320
+ ->andWhere('score', '>', 50);
321
+ })->get();
322
+
295
323
**Type**
296
324
297
- Selects documents if a field is of the specified type. For more information
325
+ Selects documents if a field is of the specified type. For more information
298
326
check: :manual:`$type </reference/operator/query/type/#op._S_type/>` in the
299
327
MongoDB Server documentation.
300
328
@@ -304,7 +332,7 @@ MongoDB Server documentation.
304
332
305
333
**Mod**
306
334
307
- Performs a modulo operation on the value of a field and selects documents with
335
+ Performs a modulo operation on the value of a field and selects documents with
308
336
a specified result.
309
337
310
338
.. code-block:: php
@@ -366,10 +394,10 @@ MongoDB-specific Geo operations
366
394
You can make a ``geoNear`` query on MongoDB.
367
395
You can omit specifying the automatic fields on the model.
368
396
The returned instance is a collection, so you can call the `Collection <https://laravel.com/docs/9.x/collections>`__ operations.
369
- Make sure that your model has a ``location`` field, and a
397
+ Make sure that your model has a ``location`` field, and a
370
398
`2ndSphereIndex <https://www.mongodb.com/docs/manual/core/2dsphere>`__.
371
399
The data in the ``location`` field must be saved as `GeoJSON <https://www.mongodb.com/docs/manual/reference/geojson/>`__.
372
- The ``location`` points must be saved as `WGS84 <https://www.mongodb.com/docs/manual/reference/glossary/#std-term-WGS84>`__
400
+ The ``location`` points must be saved as `WGS84 <https://www.mongodb.com/docs/manual/reference/glossary/#std-term-WGS84>`__
373
401
reference system for geometry calculation. That means that you must
374
402
save ``longitude and latitude``, in that order specifically, and to find near
375
403
with calculated distance, you ``must do the same way``.
@@ -405,7 +433,7 @@ with calculated distance, you ``must do the same way``.
405
433
Inserts, updates and deletes
406
434
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
407
435
408
- Inserting, updating and deleting records works just like the original Eloquent.
436
+ Inserting, updating and deleting records works just like the original Eloquent.
409
437
Please check `Laravel Docs' Eloquent section <https://laravel.com/docs/6.x/eloquent>`__.
410
438
411
439
Here, only the MongoDB-specific operations are specified.
@@ -431,14 +459,14 @@ These expressions will be injected directly into the query.
431
459
'$where' => '/.*123.*/.test(this["hyphenated-field"])',
432
460
])->get();
433
461
434
- You can also perform raw expressions on the internal MongoCollection object.
462
+ You can also perform raw expressions on the internal MongoCollection object.
435
463
If this is executed on the model class, it will return a collection of models.
436
464
437
465
If this is executed on the query builder, it will return the original response.
438
466
439
467
**Cursor timeout**
440
468
441
- To prevent ``MongoCursorTimeout`` exceptions, you can manually set a timeout
469
+ To prevent ``MongoCursorTimeout`` exceptions, you can manually set a timeout
442
470
value that will be applied to the cursor:
443
471
444
472
.. code-block:: php
0 commit comments