final class ReadPreference implements Serializable (View source)

Class ReadPreference

Constants

PRIMARY Since: 1.7.0

PRIMARY_PREFERRED Since: 1.7.0

SECONDARY Since: 1.7.0

SECONDARY_PREFERRED Since: 1.7.0

NEAREST Since: 1.7.0

NO_MAX_STALENESS Since: 1.2.0

SMALLEST_MAX_STALENESS_SECONDS Since: 1.2.0

Properties

string Since: 2.3.0
$mode
array|null Since: 2.3.0
$tags
int Since: 2.3.0
$maxStalenessSeconds
object|null deprecated Since: 2.3.0
$hedge

Methods

__construct(string $mode, array|null $tagSets = null, array|null $options = null)

Construct immutable ReadPreference

static 
__set_state(array $properties)

No description

object|null
getHedge() deprecated

Returns the ReadPreference's "hedge" option

string
getModeString()

Returns the ReadPreference's "mode" option as a string

array
getTagSets()

Returns the ReadPreference's "tagSets" option

bsonSerialize()

Returns an object for BSON serialization

getMaxStalenessSeconds()

No description

Details

final __construct(string $mode, array|null $tagSets = null, array|null $options = null)

Construct immutable ReadPreference

Parameters

string $mode

Read preference mode Value Description "primary" All operations read from the current replica set primary. This is the default read preference for MongoDB. "primaryPreferred" In most situations, operations read from the primary but if it is unavailable, operations read from secondary members. "secondary" All operations read from the secondary members of the replica set. "secondaryPreferred" In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary. "nearest" Operations read from member of the replica set with the least network latency, irrespective of the member's type.

array|null $tagSets

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 "primary" mode and, in general, only apply when selecting a secondary member of a set for a read operation. However, the "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.

array|null $options

options Option Type Description hedge objectarray Specifies whether to use hedged reads, which are supported by MongoDB 4.4+ for sharded queries. Server hedged reads are available for all non-primary read preferences and are enabled by default when using the "nearest" mode. This option allows explicitly enabling server hedged reads for non-primary read preferences by specifying ['enabled' => true], or explicitly disabling server hedged reads for the "nearest" read preference by specifying ['enabled' => false]. maxStalenessSeconds int Specifies a maximum replication lag, or "staleness", for reads from secondaries. When a secondary's estimated staleness exceeds this value, the driver stops using it for read operations. If specified, the max staleness must be a signed 32-bit integer greater than or equal to MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS. Defaults to MongoDB\Driver\ReadPreference::NO_MAX_STALENESS, which means that the driver will not consider a secondary's lag when choosing where to direct a read operation. This option is not compatible with the "primary" mode. Specifying a max staleness also requires all MongoDB instances in the deployment to be using MongoDB 3.4+. An exception will be thrown at execution time if any MongoDB instances in the deployment are of an older server version.

Exceptions

InvalidArgumentException

static __set_state(array $properties)

No description

Parameters

array $properties

final object|null getHedge() deprecated

Since: 1.8.0

deprecated

Returns the ReadPreference's "hedge" option

Return Value

object|null

final string getModeString()

Since: 1.7.0

Returns the ReadPreference's "mode" option as a string

Return Value

string

Exceptions

InvalidArgumentException

final array getTagSets()

Returns the ReadPreference's "tagSets" option

Return Value

array

final array|stdClass|Document|PackedArray bsonSerialize()

Since: 1.2.0

Returns an object for BSON serialization

Return Value

array|stdClass|Document|PackedArray

Exceptions

InvalidArgumentException

final getMaxStalenessSeconds()

No description