MONGOC_READ_PREFS_T(3)
π MONGOC_READ_PREFS_T(3) π
from libmongoc β A read preference abstraction β¨
okay bestie, welcome to the read-preference page. this is the manual for MONGOC_READ_PREFS_T(3) and itβs genuinely giving βlet me tell the driver which replica-set nodes to hit first and how.β π«Ά
β¨ SYNOPSIS β¨
mongoc_read_prefs_t provides an abstraction on top of the MongoDB connection read preferences. It allows for hinting to the driver which nodes in a replica set should be accessed first and how. π
You can specify a read preference mode on connection objects, database objects, collection objects, or per-operation. Generally, it makes the most sense to stick with the global default mode, MONGOC_READ_PRIMARY. All of the other modes come with caveats that wonβt be covered in great detail here. iykyk π
π₯ READ MODES π₯
MONGOC_READ_PRIMARYβ Default mode. All operations read from the current replica set primary. πMONGOC_READ_SECONDARYβ All operations read from among the nearest secondary members of the replica set. πΈMONGOC_READ_PRIMARY_PREFERREDβ In most situations, operations read from the primary but if it is unavailable, operations read from secondary members. πMONGOC_READ_SECONDARY_PREFERREDβ In most situations, operations read from among the nearest secondary members, but if no secondaries are available, operations read from the primary. π¦MONGOC_READ_NEARESTβ Operations read from among the nearest members of the replica set, irrespective of the memberβs type. β
π TAG SETS π
Tag sets allow you to specify custom read preferences and write concerns so that your application can target operations to specific members. periodt. π
Custom read preferences and write concerns evaluate tag sets in different ways: read preferences consider the value of a tag when selecting a member to read from, while write concerns ignore the value of a tag when selecting a member, except to consider whether or not the value is unique. the way thatβs such a specific little distinction and yet so real π
You can specify tag sets with the following read preference modes:
primaryPreferredsecondarysecondaryPreferrednearest
Tags are not compatible with MONGOC_READ_PRIMARY and, in general, only apply when selecting a secondary member of a set for a read operation. However, the nearest read mode, when combined with a tag set, will select the nearest member that matches the specified tag set, which may be a primary or secondary. π
Tag sets are represented as a comma-separated list of colon-separated key-value pairs when provided as a connection string, e.g. dc:ny,rack:1.
To specify a list of tag sets, using multiple readPreferenceTags, e.g.
readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:ny;readPreferenceTags=
Note the empty value for the last one, which means βmatch any secondary as a last resortβ. π«Ά
Order matters when using multiple readPreferenceTags. no cap.
Tag Sets can also be configured using mongoc_read_prefs_set_tags(). β¨
All interfaces use the same member selection logic to choose the member to which to direct read operations, basing the choice on read preference mode and tag sets. π§
π MAX STALENESS π
When connected to replica set running MongoDB 3.4 or later, the driver estimates the staleness of each secondary based on lastWriteDate values provided in server hello responses. π
Max Staleness is the maximum replication lag in seconds (wall clock time) that a secondary can suffer and still be eligible for reads. The default is MONGOC_NO_MAX_STALENESS, which disables staleness checks. Otherwise, it must be a positive integer at least MONGOC_SMALLEST_MAX_STALENESS_SECONDS (90 seconds). β±οΈ
Max Staleness is also supported by sharded clusters of replica sets if all servers run MongoDB 3.4 or later. π
π¦ HEDGED READS π¦
Deprecated since version MongoDB: Server 8.0 π
Hedged reads are deprecated in MongoDB version 8.0 and will be removed in a future release. bestie, thatβs your warning right there. π¨
When connecting to a sharded cluster running MongoDB 4.4 or later, reads can be sent in parallel to the two βbestβ hosts. Once one result returns, any other outstanding operations that were part of the hedged read are cancelled. β‘
When the read preference mode is MONGOC_READ_NEAREST and the sharded cluster is running MongoDB 4.4 or later, hedged reads are enabled by default. Additionally, hedged reads may be explicitly enabled or disabled by calling mongoc_read_prefs_set_hedge() with a BSON document, e.g.
{
enabled: true
}
Appropriate values for the enabled key are true or false. π
π AUTHOR π
MongoDB, Inc π
πΈ COPYRIGHT πΈ
2009-present, MongoDB, Inc. π«Ά
and thatβs the whole page, bestie. this little icon was brought to you by libmongoc, version 2.5.6, dated Oct 09, 2026. slay. π
β¨
π¦ where this page comes from
- epel 10: mongo-c-driver-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms