Mongo.ReadPreference (mongodb-driver v1.4.0) View Source

Determines which servers are considered suitable for read operations

A read preference consists of a mode and optional tags, max_staleness_ms, and hedge. The mode prioritizes between primaries and secondaries to produce either a single suitable server or a list of candidate servers. If tags and maxStalenessSeconds are set, they determine which candidate servers are eligible for selection. If hedge is set, it configures how server hedged reads are used.

The default mode is :primary. The default tags is a list with an empty tag set: [{}]. The default max_staleness_ms is unset. The default hedge is unset.

mode

  • :primary Only an available primary is suitable.
  • :secondary All secondaries (and only secondaries) are candidates, but only eligible candidates (i.e. after applying tags and maxStalenessSeconds) are suitable.
  • :primary_preferred If a primary is available, only the primary is suitable. Otherwise, all secondaries are candidates, but only eligible secondaries are suitable.
  • :secondary_preferred All secondaries are candidates. If there is at least one eligible secondary, only eligible secondaries are suitable. Otherwise, when there are no eligible secondaries, the primary is suitable.
  • :nearest The primary and all secondaries are candidates, but only eligible candidates are suitable.

Link to this section Summary

Functions

Add read preference to the cmd

Merge default values to the read preferences and converts deprecated tag_sets to tags

Converts the preference to the mongodb format for mongos

Converts the preference to the mongodb format for replica sets

Link to this section Functions

Link to this function

add_read_preference(cmd, opts)

View Source

Add read preference to the cmd

Merge default values to the read preferences and converts deprecated tag_sets to tags

Converts the preference to the mongodb format for mongos

Converts the preference to the mongodb format for replica sets