read_preferences – Utilities for choosing which member of a replica set to read from. (original) (raw)

Utilities for choosing which member of a replica set to read from.

See also

This module is compatible with both the synchronous and asynchronous PyMongo APIs.

class pymongo.read_preferences.Primary

Primary read preference.

document

Read preference as a document.

mode

The mode of this read preference instance.

name

The name of this read preference.

class pymongo.read_preferences.PrimaryPreferred(tag_sets=None, max_staleness=-1, hedge=None)

PrimaryPreferred read preference.

Note

When a MongoClient is first created reads will be routed to an available secondary until the primary of the replica set is discovered.

Parameters:

Changed in version 3.11: Added hedge parameter.

property document_: dict[str, Any]_

Read preference as a document.

property hedge_: Mapping[str, Any] | None_

DEPRECATED - The read preference ‘hedge’ option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for ‘hedge’ will be removed in PyMongo 5.0.

The read preference hedge parameter.

A dictionary that configures how the server will perform hedged reads. It consists of the following keys:

Hedged reads are automatically enabled in MongoDB 4.4+ when using anearest read preference. To explicitly enable hedged reads, set the enabled key to true:

Nearest(hedge={'enabled': True})

To explicitly disable hedged reads, set the enabled key toFalse:

Nearest(hedge={'enabled': False})

Added in version 3.11.

property max_staleness_: int_

The maximum estimated length of time (in seconds) a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations, or -1 for no maximum.

property min_wire_version_: int_

The wire protocol version the server must support.

Some read preferences impose version requirements on all servers (e.g. maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).

All servers’ maxWireVersion must be at least this read preference’smin_wire_version, or the driver raisesConfigurationError.

property mode_: int_

The mode of this read preference instance.

property mongos_mode_: str_

The mongos mode of this read preference.

property name_: str_

The name of this read preference.

property tag_sets_: Sequence[Mapping[str, Any]]_

Set tag_sets to a list of dictionaries like [{‘dc’: ‘ny’}] to read only from members whose dc tag has the value "ny". To specify a priority-order for tag sets, provide a list of tag sets: [{'dc': 'ny'}, {'dc': 'la'}, {}]. A final, empty tag set, {}, means “read from any member that matches the mode, ignoring tags.” MongoClient tries each set of tags in turn until it finds a set of tags with at least one matching member. For example, to only send a query to an analytic node:

Nearest(tag_sets=[{"node":"analytics"}])

Or using SecondaryPreferred:

SecondaryPreferred(tag_sets=[{"node":"analytics"}])

.. seealso:: Data-Center Awareness <https://www.mongodb.com/docs/manual/data-center-awareness/>_

class pymongo.read_preferences.Secondary(tag_sets=None, max_staleness=-1, hedge=None)

Secondary read preference.

Parameters:

Changed in version 3.11: Added hedge parameter.

property document_: dict[str, Any]_

Read preference as a document.

property hedge_: Mapping[str, Any] | None_

DEPRECATED - The read preference ‘hedge’ option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for ‘hedge’ will be removed in PyMongo 5.0.

The read preference hedge parameter.

A dictionary that configures how the server will perform hedged reads. It consists of the following keys:

Hedged reads are automatically enabled in MongoDB 4.4+ when using anearest read preference. To explicitly enable hedged reads, set the enabled key to true:

Nearest(hedge={'enabled': True})

To explicitly disable hedged reads, set the enabled key toFalse:

Nearest(hedge={'enabled': False})

Added in version 3.11.

property max_staleness_: int_

The maximum estimated length of time (in seconds) a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations, or -1 for no maximum.

property min_wire_version_: int_

The wire protocol version the server must support.

Some read preferences impose version requirements on all servers (e.g. maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).

All servers’ maxWireVersion must be at least this read preference’smin_wire_version, or the driver raisesConfigurationError.

property mode_: int_

The mode of this read preference instance.

property mongos_mode_: str_

The mongos mode of this read preference.

property name_: str_

The name of this read preference.

property tag_sets_: Sequence[Mapping[str, Any]]_

Set tag_sets to a list of dictionaries like [{‘dc’: ‘ny’}] to read only from members whose dc tag has the value "ny". To specify a priority-order for tag sets, provide a list of tag sets: [{'dc': 'ny'}, {'dc': 'la'}, {}]. A final, empty tag set, {}, means “read from any member that matches the mode, ignoring tags.” MongoClient tries each set of tags in turn until it finds a set of tags with at least one matching member. For example, to only send a query to an analytic node:

Nearest(tag_sets=[{"node":"analytics"}])

Or using SecondaryPreferred:

SecondaryPreferred(tag_sets=[{"node":"analytics"}])

.. seealso:: Data-Center Awareness <https://www.mongodb.com/docs/manual/data-center-awareness/>_

class pymongo.read_preferences.SecondaryPreferred(tag_sets=None, max_staleness=-1, hedge=None)

SecondaryPreferred read preference.

Note

When a MongoClient is first created reads will be routed to the primary of the replica set until an available secondary is discovered.

Parameters:

Changed in version 3.11: Added hedge parameter.

property document_: dict[str, Any]_

Read preference as a document.

property hedge_: Mapping[str, Any] | None_

DEPRECATED - The read preference ‘hedge’ option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for ‘hedge’ will be removed in PyMongo 5.0.

The read preference hedge parameter.

A dictionary that configures how the server will perform hedged reads. It consists of the following keys:

Hedged reads are automatically enabled in MongoDB 4.4+ when using anearest read preference. To explicitly enable hedged reads, set the enabled key to true:

Nearest(hedge={'enabled': True})

To explicitly disable hedged reads, set the enabled key toFalse:

Nearest(hedge={'enabled': False})

Added in version 3.11.

property max_staleness_: int_

The maximum estimated length of time (in seconds) a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations, or -1 for no maximum.

property min_wire_version_: int_

The wire protocol version the server must support.

Some read preferences impose version requirements on all servers (e.g. maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).

All servers’ maxWireVersion must be at least this read preference’smin_wire_version, or the driver raisesConfigurationError.

property mode_: int_

The mode of this read preference instance.

property mongos_mode_: str_

The mongos mode of this read preference.

property name_: str_

The name of this read preference.

property tag_sets_: Sequence[Mapping[str, Any]]_

Set tag_sets to a list of dictionaries like [{‘dc’: ‘ny’}] to read only from members whose dc tag has the value "ny". To specify a priority-order for tag sets, provide a list of tag sets: [{'dc': 'ny'}, {'dc': 'la'}, {}]. A final, empty tag set, {}, means “read from any member that matches the mode, ignoring tags.” MongoClient tries each set of tags in turn until it finds a set of tags with at least one matching member. For example, to only send a query to an analytic node:

Nearest(tag_sets=[{"node":"analytics"}])

Or using SecondaryPreferred:

SecondaryPreferred(tag_sets=[{"node":"analytics"}])

.. seealso:: Data-Center Awareness <https://www.mongodb.com/docs/manual/data-center-awareness/>_

class pymongo.read_preferences.Nearest(tag_sets=None, max_staleness=-1, hedge=None)

Nearest read preference.

Parameters:

Changed in version 3.11: Added hedge parameter.

property document_: dict[str, Any]_

Read preference as a document.

property hedge_: Mapping[str, Any] | None_

DEPRECATED - The read preference ‘hedge’ option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for ‘hedge’ will be removed in PyMongo 5.0.

The read preference hedge parameter.

A dictionary that configures how the server will perform hedged reads. It consists of the following keys:

Hedged reads are automatically enabled in MongoDB 4.4+ when using anearest read preference. To explicitly enable hedged reads, set the enabled key to true:

Nearest(hedge={'enabled': True})

To explicitly disable hedged reads, set the enabled key toFalse:

Nearest(hedge={'enabled': False})

Added in version 3.11.

property max_staleness_: int_

The maximum estimated length of time (in seconds) a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations, or -1 for no maximum.

property min_wire_version_: int_

The wire protocol version the server must support.

Some read preferences impose version requirements on all servers (e.g. maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).

All servers’ maxWireVersion must be at least this read preference’smin_wire_version, or the driver raisesConfigurationError.

property mode_: int_

The mode of this read preference instance.

property mongos_mode_: str_

The mongos mode of this read preference.

property name_: str_

The name of this read preference.

property tag_sets_: Sequence[Mapping[str, Any]]_

Set tag_sets to a list of dictionaries like [{‘dc’: ‘ny’}] to read only from members whose dc tag has the value "ny". To specify a priority-order for tag sets, provide a list of tag sets: [{'dc': 'ny'}, {'dc': 'la'}, {}]. A final, empty tag set, {}, means “read from any member that matches the mode, ignoring tags.” MongoClient tries each set of tags in turn until it finds a set of tags with at least one matching member. For example, to only send a query to an analytic node:

Nearest(tag_sets=[{"node":"analytics"}])

Or using SecondaryPreferred:

SecondaryPreferred(tag_sets=[{"node":"analytics"}])

.. seealso:: Data-Center Awareness <https://www.mongodb.com/docs/manual/data-center-awareness/>_

class pymongo.read_preferences.ReadPreference

An enum that defines some commonly used read preference modes.

Apps can also create a custom read preference, for example:

Nearest(tag_sets=[{"node":"analytics"}])

See High Availability and PyMongo for code examples.

A read preference is used in three cases:

MongoClient connected to a single mongod:

MongoClient initialized with thereplicaSet option:

MongoClient connected to a mongos, with a sharded cluster of replica sets:

PRIMARY = Primary()

Parameters:

selection (Selection)

Return type:

Selection

PRIMARY_PREFERRED = PrimaryPreferred(tag_sets=None, max_staleness=-1, hedge=None)

Parameters:

selection (Selection)

Return type:

Selection

SECONDARY = Secondary(tag_sets=None, max_staleness=-1, hedge=None)

Parameters:

selection (Selection)

Return type:

Selection

SECONDARY_PREFERRED = SecondaryPreferred(tag_sets=None, max_staleness=-1, hedge=None)

Parameters:

selection (Selection)

Return type:

Selection

NEAREST = Nearest(tag_sets=None, max_staleness=-1, hedge=None)

Parameters:

selection (Selection)

Return type:

Selection