Docs Menu
Docs Home
/ / /
Laravel MongoDB
/ /

Set a Read Preference

On this page

  • Overview
  • Before You Get Started
  • Set a Read Preference
  • Additional Information

In this guide, you can learn how to set a read preference when performing find operations with Laravel MongoDB.

To run the code examples in this guide, complete the Quick Start tutorial. This tutorial provides instructions on setting up a MongoDB Atlas instance with sample data and creating the following files in your Laravel web application:

  • Movie.php file, which contains a Movie model to represent documents in the movies collection

  • MovieController.php file, which contains a show() function to run database operations

  • browse_movies.blade.php file, which contains HTML code to display the results of database operations

The following sections describe how to edit the files in your Laravel application to run the find operation code examples and view the expected output.

To specify which replica set members receive your read operations, set a read preference by using the readPreference() method.

The readPreference() method accepts the following parameters:

  • mode: (Required) A string value specifying the read preference mode.

  • tagSets: (Optional) An array value specifying key-value tags that correspond to certain replica set members.

  • options: (Optional) An array value specifying additional read preference options.

Tip

To view a full list of available read preference modes and options, see MongoDB\Driver\ReadPreference::__construct in the MongoDB PHP extension documentation.

The following example queries for documents in which the value of the title field is "Carrie" and sets the read preference to ReadPreference::SECONDARY_PREFERRED. As a result, the query retrieves the results from secondary replica set members or the primary member if no secondaries are available:

Use the following syntax to specify the query:

$movies = Movie::where('title', 'Carrie')
->readPreference(ReadPreference::SECONDARY_PREFERRED)
->get();

To see the query results in the browse_movies view, edit the show() function in the MovieController.php file to resemble the following code:

class MovieController
{
public function show()
{
$movies = Movie::where('title', 'Carrie')
->readPreference(ReadPreference::SECONDARY_PREFERRED)
->get();
return view('browse_movies', [
'movies' => $movies
]);
}
}
Title: Carrie
Year: 1952
Runtime: 118
IMDB Rating: 7.5
IMDB Votes: 1458
Plot: Carrie boards the train to Chicago with big ambitions. She gets a
job stitching shoes and her sister's husband takes almost all of her pay
for room and board. Then she injures a finger and ...
Title: Carrie
Year: 1976
Runtime: 98
IMDB Rating: 7.4
IMDB Votes: 115528
Plot: A shy, outcast 17-year old girl is humiliated by her classmates for the
last time.
Title: Carrie
Year: 2002
Runtime: 132
IMDB Rating: 5.5
IMDB Votes: 7412
Plot: Carrie White is a lonely and painfully shy teenage girl with telekinetic
powers who is slowly pushed to the edge of insanity by frequent bullying from
both her classmates and her domineering, religious mother.
Title: Carrie
Year: 2013
Runtime: 100
IMDB Rating: 6
IMDB Votes: 98171
Plot: A reimagining of the classic horror tale about Carrie White, a shy girl
outcast by her peers and sheltered by her deeply religious mother, who unleashes
telekinetic terror on her small town after being pushed too
far at her senior prom.

To learn how to retrieve data based on filter criteria, see the Retrieve Data guide.

To learn how to modify the way that the Laravel Integration returns results, see the Modify Query Results guide.

Back

Modify Query Results