Skip to content

Commit 89b4a41

Browse files
committed
DOCSP-46438: Read preference
1 parent af50a44 commit 89b4a41

File tree

4 files changed

+160
-4
lines changed

4 files changed

+160
-4
lines changed

docs/fundamentals/read-operations.txt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ method:
359359
results in a specified order based on field values
360360
- :ref:`laravel-retrieve-one` uses the ``first()`` method to return the first document
361361
that matches the query filter
362+
- :ref:`laravel-read-pref` uses the ``readPreference()`` method to direct the query
363+
to specific replica set members
362364

363365
.. _laravel-skip-limit:
364366

@@ -606,3 +608,109 @@ field.
606608

607609
To learn more about the ``orderBy()`` method, see the
608610
:ref:`laravel-sort` section of this guide.
611+
612+
.. _laravel-read-pref:
613+
614+
Set a Read Preference
615+
~~~~~~~~~~~~~~~~~~~~~
616+
617+
To specify which replica set members receive your read operation,
618+
set a read preference by using the ``readPreference()`` method.
619+
620+
The ``readPreference()`` method accepts the following parameters:
621+
622+
- ``mode``: (Required) A string value specifying the read preference
623+
mode.
624+
625+
- ``tagSets``: (Optional) An array value specifying key-value tags that correspond to
626+
the target replica set members.
627+
628+
- ``options``: (Optional) An array value specifying additional read preference options.
629+
630+
.. tip::
631+
632+
To view a full list of available read preference modes and options, see
633+
:php:`MongoDB\Driver\ReadPreference::__construct </manual/en/mongodb-driver-readpreference.construct.php>`
634+
in the MongoDB PHP extension documentation.
635+
636+
The following example queries for documents in which the value of the ``title``
637+
field is ``"Carrie"`` and retrieves the results from secondary replica set
638+
members, or the primary member if no secondaries are available:
639+
640+
.. tabs::
641+
642+
.. tab:: Query Syntax
643+
:tabid: query-syntax
644+
645+
Use the following syntax to specify the query:
646+
647+
.. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
648+
:language: php
649+
:dedent:
650+
:start-after: start-read-pref
651+
:end-before: end-read-pref
652+
653+
.. tab:: Controller Method
654+
:tabid: controller
655+
656+
To see the query results in the ``browse_movies`` view, edit the ``show()`` function
657+
in the ``MovieController.php`` file to resemble the following code:
658+
659+
.. io-code-block::
660+
:copyable: true
661+
662+
.. input::
663+
:language: php
664+
665+
class MovieController
666+
{
667+
public function show()
668+
{
669+
$movies = Movie::where('title', 'Carrie')
670+
->readPreference('secondaryPreferred')
671+
->get();
672+
673+
return view('browse_movies', [
674+
'movies' => $movies
675+
]);
676+
}
677+
}
678+
679+
.. output::
680+
:language: none
681+
:visible: false
682+
683+
Title: Carrie
684+
Year: 1952
685+
Runtime: 118
686+
IMDB Rating: 7.5
687+
IMDB Votes: 1458
688+
Plot: Carrie boards the train to Chicago with big ambitions. She gets a
689+
job stitching shoes and her sister's husband takes almost all of her pay
690+
for room and board. Then she injures a finger and ...
691+
692+
Title: Carrie
693+
Year: 1976
694+
Runtime: 98
695+
IMDB Rating: 7.4
696+
IMDB Votes: 115528
697+
Plot: A shy, outcast 17-year old girl is humiliated by her classmates for the
698+
last time.
699+
700+
Title: Carrie
701+
Year: 2002
702+
Runtime: 132
703+
IMDB Rating: 5.5
704+
IMDB Votes: 7412
705+
Plot: Carrie White is a lonely and painfully shy teenage girl with telekinetic
706+
powers who is slowly pushed to the edge of insanity by frequent bullying from
707+
both her classmates and her domineering, religious mother.
708+
709+
Title: Carrie
710+
Year: 2013
711+
Runtime: 100
712+
IMDB Rating: 6
713+
IMDB Votes: 98171
714+
Plot: A reimagining of the classic horror tale about Carrie White, a shy girl
715+
outcast by her peers and sheltered by her deeply religious mother, who unleashes
716+
telekinetic terror on her small town after being pushed too far at her senior prom.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,20 @@ public function arrayElemMatch(): void
164164
$this->assertNotNull($movies);
165165
$this->assertCount(2, $movies);
166166
}
167+
168+
/**
169+
* @runInSeparateProcess
170+
* @preserveGlobalState disabled
171+
*/
172+
public function testReadPreference(): void
173+
{
174+
// start-read-pref
175+
$movies = Movie::where('title', 'Carrie')
176+
->readPreference('secondaryPreferred')
177+
->get();
178+
// end-read-pref
179+
180+
$this->assertNotNull($movies);
181+
$this->assertCount(4, $movies);
182+
}
167183
}

docs/includes/query-builder/QueryBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,18 @@ public function testCursorTimeout(): void
452452
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
453453
}
454454

455+
public function testReadPreference(): void
456+
{
457+
// begin query read pref
458+
$result = DB::table('movies')
459+
->where('runtime', '>', 240)
460+
->readPreference('secondary')
461+
->get();
462+
// end query read pref
463+
464+
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
465+
}
466+
455467
public function testNear(): void
456468
{
457469
$this->importTheaters();

docs/query-builder.txt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ to use the following MongoDB-specific query operations:
840840
- :ref:`Run MongoDB Query API operations <laravel-query-builder-whereRaw>`
841841
- :ref:`Match documents that contain array elements <laravel-query-builder-elemMatch>`
842842
- :ref:`Specify a cursor timeout <laravel-query-builder-cursor-timeout>`
843+
- :ref:`Specify a read preference <>`
843844
- :ref:`Match locations by using geospatial searches <laravel-query-builder-geospatial>`
844845

845846
.. _laravel-query-builder-exists:
@@ -1033,6 +1034,25 @@ to specify a maximum duration to wait for cursor operations to complete.
10331034
`MongoDB\Collection::find() <https://www.mongodb.com/docs/php-library/current/reference/method/MongoDBCollection-find/>`__
10341035
in the PHP Library documentation.
10351036

1037+
.. _laravel-query-builder-read-pref:
1038+
1039+
Specify a Read Preference Example
1040+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1041+
1042+
You can control how the {+odm-short+} directs read operations to replica
1043+
set members by setting a read preference.
1044+
1045+
The following example queries the ``movie`` collection for documents
1046+
in which the ``runtime`` value is greater than ``240``. The example passes a
1047+
value of ``'secondary'`` to the ``readPreference()`` method, which sends
1048+
the query to secondary replica set members:
1049+
1050+
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
1051+
:language: php
1052+
:dedent:
1053+
:start-after: begin query read pref
1054+
:end-before: end query read pref
1055+
10361056
.. _laravel-query-builder-geospatial:
10371057

10381058
Match Locations by Using Geospatial Operations
@@ -1061,7 +1081,7 @@ in the {+server-docs-name+}.
10611081
.. _laravel-query-builder-geospatial-near:
10621082

10631083
Near a Position Example
1064-
~~~~~~~~~~~~~~~~~~~~~~~
1084+
^^^^^^^^^^^^^^^^^^^^^^^
10651085

10661086
The following example shows how to use the ``near`` query operator
10671087
with the ``where()`` query builder method to match documents that
@@ -1081,7 +1101,7 @@ in the {+server-docs-name+}.
10811101
.. _laravel-query-builder-geospatial-geoWithin:
10821102

10831103
Within an Area Example
1084-
~~~~~~~~~~~~~~~~~~~~~~
1104+
^^^^^^^^^^^^^^^^^^^^^^
10851105

10861106
The following example shows how to use the ``geoWithin``
10871107
query operator with the ``where()``
@@ -1098,7 +1118,7 @@ GeoJSON object:
10981118
.. _laravel-query-builder-geospatial-geoIntersects:
10991119

11001120
Intersecting a Geometry Example
1101-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1121+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11021122

11031123
The following example shows how to use the ``geoInstersects``
11041124
query operator with the ``where()`` query builder method to
@@ -1114,7 +1134,7 @@ the specified ``LineString`` GeoJSON object:
11141134
.. _laravel-query-builder-geospatial-geoNear:
11151135

11161136
Proximity Data for Nearby Matches Example
1117-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1137+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11181138

11191139
The following example shows how to use the ``geoNear`` aggregation operator
11201140
with the ``raw()`` query builder method to perform an aggregation that returns

0 commit comments

Comments
 (0)