The MongoDB\Driver\ReadPreference class

- « MongoDB\Driver\WriteConcern::unserialize

- MongoDB\Driver\ReadPreference::bsonSerialize »

- PHP Manual
- MongoDB\Driver
- The MongoDB\Driver\ReadPreference class

# The MongoDB\Driver\ReadPreference class

(mongodb >=1.0.0)

## Introduction

## Class synopsis

final class MongoDB\Driver\ReadPreference implements MongoDB\BSON\Serializable, Serializable {
/* Constants */
const int RP_PRIMARY = 1;
const int RP_PRIMARY_PREFERRED = 5;
const int RP_SECONDARY = 2;
const int RP_SECONDARY_PREFERRED = 6;
const int RP_NEAREST = 10;
const string PRIMARY = primary;
const string PRIMARY_PREFERRED = primaryPreferred;
const string SECONDARY = secondary;
const string SECONDARY_PREFERRED = secondaryPreferred;
const string NEAREST = nearest;
const int NO_MAX_STALENESS = -1;
const int SMALLEST_MAX_STALENESS_SECONDS = 90;
/* Methods */
final public bsonSerialize(): stdClass
final public __construct(string|int `$mode`, ?array `$tagSets` = `null`, ?array `$options` = `null`)
final public getHedge(): ?object
final public getMaxStalenessSeconds(): int
final public getMode(): int
final public getModeString(): string
final public getTagSets(): array
final public serialize(): string
final public unserialize(string `$data`): void
}

## Predefined Constants

`MongoDB\Driver\ReadPreference::RP_PRIMARY`

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

  Warning

  This constant has been DEPRECATED as of extension version 1.20.0 and will be removed in 2.0. Applications should use `MongoDB\Driver\ReadPreference::PRIMARY` instead.

`MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED`

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

  Warning

  This constant has been DEPRECATED as of extension version 1.20.0 and will be removed in 2.0. Applications should use `MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED` instead.

`MongoDB\Driver\ReadPreference::RP_SECONDARY`

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

  Warning

  This constant has been DEPRECATED as of extension version 1.20.0 and will be removed in 2.0. Applications should use `MongoDB\Driver\ReadPreference::SECONDARY` instead.

`MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED`

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

  Warning

  This constant has been DEPRECATED as of extension version 1.20.0 and will be removed in 2.0. Applications should use `MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED` instead.

`MongoDB\Driver\ReadPreference::RP_NEAREST`

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

  Warning

  This constant has been DEPRECATED as of extension version 1.20.0 and will be removed in 2.0. Applications should use `MongoDB\Driver\ReadPreference::NEAREST` instead.

`MongoDB\Driver\ReadPreference::PRIMARY`

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

`MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED`

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

`MongoDB\Driver\ReadPreference::SECONDARY`

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

`MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED`

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

`MongoDB\Driver\ReadPreference::NEAREST`

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

`MongoDB\Driver\ReadPreference::NO_MAX_STALENESS`

  The default value for the `"maxStalenessSeconds"` option is to specify no limit on maximum staleness, which means that the driver will not consider a secondary's lag when choosing where to direct a read operation.

`MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS`

  The minimum value for the `"maxStalenessSeconds"` option is 90 seconds. The driver estimates secondaries' staleness by periodically checking the latest write date of each replica set member. Since these checks are infrequent, the staleness estimate is coarse. Thus, the driver cannot enforce a max staleness value of less than 90 seconds.

## Changelog

Version | Description
--- | ---
PECL mongodb 1.20.0 | Deprecated the `MongoDB\Driver\ReadPreference::RP_PRIMARY`, `MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED`, `MongoDB\Driver\ReadPreference::RP_SECONDARY`, `MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED`, and `MongoDB\Driver\ReadPreference::RP_NEAREST` constants.
PECL mongodb 1.7.0 | Added the `MongoDB\Driver\ReadPreference::PRIMARY`, `MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED`, `MongoDB\Driver\ReadPreference::SECONDARY`, `MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED`, and `MongoDB\Driver\ReadPreference::NEAREST` constants. Implements Serializable.
PECL mongodb 1.2.0 | Added the `MongoDB\Driver\ReadPreference::NO_MAX_STALENESS` and `MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS` constants. Implements MongoDB\BSON\Serializable.

## Table of Contents

- MongoDB\Driver\ReadPreference::bsonSerialize — Returns an object for BSON serialization
- MongoDB\Driver\ReadPreference::__construct — Create a new ReadPreference
- MongoDB\Driver\ReadPreference::getHedge — Returns the ReadPreference's "hedge" option
- MongoDB\Driver\ReadPreference::getMaxStalenessSeconds — Returns the ReadPreference's "maxStalenessSeconds" option
- MongoDB\Driver\ReadPreference::getMode — Returns the ReadPreference's "mode" option
- MongoDB\Driver\ReadPreference::getModeString — Returns the ReadPreference's "mode" option
- MongoDB\Driver\ReadPreference::getTagSets — Returns the ReadPreference's "tagSets" option
- MongoDB\Driver\ReadPreference::serialize — Serialize a ReadPreference
- MongoDB\Driver\ReadPreference::unserialize — Unserialize a ReadPreference