Please wait while we load your page...



PHP Manual [MongoDB\Driver\ReadPreference

MongoDB\Driver\ReadPreference || MongoDB\Driver\ReadPreference::getMode || PHP Manual

(mongodb >=1.0.0)

MongoDB\Driver\ReadPreference::__constructConstruct immutable ReadPreference


final public MongoDB\Driver\ReadPreference::__construct ( string $mode [, array $tagSets ] )

Creates a new ReadPreference.



Read preference mode
Value Description

All operations read from the current replica set primary. This is the default read preference for MongoDB.


In most situations, operations read from the primary but if it is unavailable, operations read from secondary members.


All operations read from the secondary members of the replica set.


In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary.


Operations read from member of the replica set with the least network latency, irrespective of the member's type.


Tag sets allow you to target read operations to specific members of a replica set. This parameter should be an array of associative arrays, each of which contain zero or more key/value pairs. When selecting a server for a read operation, the driver attempt to select a node having all tags in a set (i.e. the associative array of key/value pairs). If selection fails, the driver will attempt subsequent sets. An empty tag set (array()) will match any node and may be used as a fallback.

Tags are not compatible with the MongoDB\Driver\ReadPreference::RP_PRIMARY mode and, in general, only apply when selecting a secondary member of a set for a read operation. However, the MongoDB\Driver\ReadPreference::RP_NEAREST mode, when combined with a tag set, selects the matching member with the lowest network latency. This member may be a primary or secondary.



Example #1 MongoDB\Driver\ReadPreference::__construct() example


/* Prefer a secondary node but fall back to a primary. */
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED);

/* Prefer a node in the New York data center with lowest latency. */
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_NEAREST, [['dc' => 'ny']]);


MongoDB\Driver\ReadPreference || MongoDB\Driver\ReadPreference::getMode || PHP Manual
Live Chat Not Available