MongoDB C++ Driver  mongocxx-3.4.0
Classes | Public Types | Public Member Functions | List of all members
mongocxx::read_preference Class Reference

Class representing a preference for how the driver routes read operations to members of a replica set or to a sharded cluster. More...

#include <read_preference.hpp>

Classes

struct  deprecated_tag
 

Public Types

enum  read_mode : std::uint8_t {
  read_mode::k_primary, read_mode::k_primary_preferred, read_mode::k_secondary, read_mode::k_secondary_preferred,
  read_mode::k_nearest
}
 Determines which members in a replica set are acceptable to read from. More...
 

Public Member Functions

 read_preference ()
 Constructs a new read_preference with read_mode set to k_primary.
 
MONGOCXX_DEPRECATED read_preference (read_mode mode)
 Constructs a new read_preference. More...
 
MONGOCXX_DEPRECATED read_preference (read_mode mode, bsoncxx::document::view_or_value tags)
 Constructs a new read_preference with tags. More...
 
 read_preference (const read_preference &)
 Copy constructs a read_preference.
 
read_preferenceoperator= (const read_preference &)
 Copy assigns a read_preference.
 
 read_preference (read_preference &&) noexcept
 Move constructs a read_preference.
 
read_preferenceoperator= (read_preference &&) noexcept
 Move assigns a read_preference.
 
 ~read_preference ()
 Destroys a read_preference.
 
read_preferencemode (read_mode mode)
 Sets a new mode for this read_preference. More...
 
read_mode mode () const
 Returns the current read_mode for this read_preference. More...
 
read_preferencetags (bsoncxx::document::view_or_value tags)
 Sets or updates the tags for this read_preference. More...
 
stdx::optional< bsoncxx::document::viewtags () const
 Returns the current tags for this read_preference. More...
 
read_preferencemax_staleness (std::chrono::seconds max_staleness)
 Sets the max staleness setting for this read_preference. More...
 
stdx::optional< std::chrono::seconds > max_staleness () const
 Returns the current max staleness setting for this read_preference. More...
 

Friends

bool operator== (const read_preference &, const read_preference &)
 Compares two read_preference objects for (in)-equality.
 
bool operator!= (const read_preference &, const read_preference &)
 Compares two read_preference objects for (in)-equality.
 

Detailed Description

Class representing a preference for how the driver routes read operations to members of a replica set or to a sharded cluster.

By default read operations are directed to the primary member in a replica set. Reading from the primary guarantees that read operations reflect the latest version of a document. However, by distributing some or all reads to secondary members of the replica set, you can improve read throughput or reduce latency for an application that does not require fully up-to-date data.

Read preference can be broadly specified by setting a mode. It is also possible to set tags in the read preference for more granular control, and to target specific members of a replica set via attributes other than their current state as a primary or secondary node. Furthermore, it is also possible to set a staleness threshold, such that the read is limited to targeting secondaries whose staleness is less than or equal to the given threshold.

Read preferences are ignored for direct connections to a single mongod instance. However, in order to perform read operations on a direct connection to a secondary member of a replica set, you must set a read preference that allows reading from secondaries.

See also
https://docs.mongodb.com/master/core/read-preference/
Examples:
examples/mongocxx/client_session.cpp.

Member Enumeration Documentation

◆ read_mode

enum mongocxx::read_preference::read_mode : std::uint8_t
strong

Determines which members in a replica set are acceptable to read from.

Warning
Read preference tags are not respected when the mode is set to primary.
All read preference modes except primary may return stale data because secondaries replicate operations from the primary with some delay. Ensure that your application can tolerate stale data if you choose to use a non-primary mode.
See also
https://docs.mongodb.com/master/core/read-preference/#read-preference-modes
Enumerator
k_primary 

Only read from a primary node.

k_primary_preferred 

Prefer to read from a primary node.

k_secondary 

Only read from secondary nodes.

k_secondary_preferred 

Prefer to read from secondary nodes.

k_nearest 

Read from the node with the lowest latency irrespective of state.

Constructor & Destructor Documentation

◆ read_preference() [1/2]

MONGOCXX_DEPRECATED mongocxx::read_preference::read_preference ( read_mode  mode)

Constructs a new read_preference.

Parameters
modeSspecifies the read_mode.
Deprecated:
The constructor with no arguments and the method mode() should be used.

◆ read_preference() [2/2]

MONGOCXX_DEPRECATED mongocxx::read_preference::read_preference ( read_mode  mode,
bsoncxx::document::view_or_value  tags 
)

Constructs a new read_preference with tags.

Parameters
modeA read_preference read_mode.
tagsA document representing tags to use for the read_preference.
See also
https://docs.mongodb.com/master/core/read-preference/#tag-sets
Deprecated:
The tags() method should be used instead.

Member Function Documentation

◆ max_staleness() [1/2]

read_preference& mongocxx::read_preference::max_staleness ( std::chrono::seconds  max_staleness)

Sets the max staleness setting for this read_preference.

Secondary servers with an estimated lag greater than this value will be excluded from selection under modes that allow secondaries.

Max staleness must be at least 90 seconds, and also at least the sum (in seconds) of the client's heartbeatFrequencyMS and the server's idle write period, which is 10 seconds. For general use, 90 seconds is the effective minimum. If less, an exception will be thrown when an operation is attempted.

Max staleness may only be used with MongoDB version 3.4 or later. If used with an earlier version, an exception will be thrown when an operation is attempted.

Note
The max-staleness feature is designed to prevent badly-lagging servers from being selected. The staleness estimate is imprecise and shouldn't be used to try to select "up-to-date" secondaries.
Parameters
max_stalenessThe new max staleness setting. It must be positive.
Returns
A reference to the object on which this member function is being called. This facilitates method chaining.
Exceptions
mongocxx::logic_errorif the argument is invalid.

◆ max_staleness() [2/2]

stdx::optional<std::chrono::seconds> mongocxx::read_preference::max_staleness ( ) const

Returns the current max staleness setting for this read_preference.

Returns
The optionally current max staleness setting.

◆ mode() [1/2]

read_preference& mongocxx::read_preference::mode ( read_mode  mode)

Sets a new mode for this read_preference.

Parameters
modeThe new read preference mode.
Returns
A reference to the object on which this member function is being called. This facilitates method chaining.
Examples:
examples/mongocxx/client_session.cpp.

◆ mode() [2/2]

read_mode mongocxx::read_preference::mode ( ) const

Returns the current read_mode for this read_preference.

Returns
The current read_mode.

◆ tags() [1/2]

read_preference& mongocxx::read_preference::tags ( bsoncxx::document::view_or_value  tags)

Sets or updates the tags for this read_preference.

Parameters
tagsDocument representing the tags.
See also
https://docs.mongodb.com/master/core/read-preference/#tag-sets
Returns
A reference to the object on which this member function is being called. This facilitates method chaining.

◆ tags() [2/2]

stdx::optional<bsoncxx::document::view> mongocxx::read_preference::tags ( ) const

Returns the current tags for this read_preference.

Returns
The optionally set current tags.
See also
https://docs.mongodb.com/master/core/read-preference/#tag-sets

The documentation for this class was generated from the following file: