cursor.readPref() (original) (raw)

cursor.readPref(mode, tagSet, hedgeOptions)

Important

mongosh Method

This page documents a mongosh method. This is _not_the documentation for a language-specific driver, such as Node.js.

For MongoDB API drivers, refer to the language-specificMongoDB driver documentation.

Append readPref() to a cursor to control how the client routes the query to members of the replica set.

Note

You must apply readPref() to the cursor before retrieving any documents from the database.

Parameter Type Description
mode string One of the following read preference modes: primary,primaryPreferred, secondary,secondaryPreferred, or nearest
tagSet array of documents Optional. A tag set used to target reads to members with the specified tag(s). tagSet is not available if using primary.For details, see Read Preference Tag Set Lists.
hedgeOptions document IMPORTANT: Starting in MongoDB 8.0, hedged reads are deprecated. Queries that specify the read preferencenearest no longer use hedged reads by default. If you explicitly specify a hedged read, MongoDB performs a hedged read and logs a warning.Optional. A document that specifies whether to enable the use ofhedged reads.{ enabled: }The enabled field defaults to true; i.e. specifying an empty document { } is equivalent to specifying { enabled: true }.

readPref() does not support theRead Preference maxStalenessSeconds option for read preference.

This method is available in deployments hosted in the following environments:

Note

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, seeUnsupported Commands.

The following operation uses the read preference mode to target the read to a secondary member.


db.collection.find({ }).readPref( "secondary")

To target secondaries with specific tags, include both the mode and the tagSetarray:


db.collection.find({ }).readPref(

   "secondary",

   [

      { "datacenter": "B" },    // First, try matching by the datacenter tag

      { "region": "West"},      // If not found, then try matching by the region tag

      { }                       // If not found, then use the empty document to match all eligible members

   ]

)

During the secondary selection process, MongoDB tries to find secondary members with the datacenter: "B" tag first.

See Order of Tag Matching for details.

See also:

For sharded clusters, you can enable hedged readsfor non-primary read preference. To use hedged reads, the mongos must have enabled support for hedged reads (the default) and the non-primary read preferences must enable the use of hedged reads.

To target secondaries on sharded clusters using hedged reads, include both the mode and thehedgeOptions, as in the following examples:

db.collection.find({ }).readPref(  
   "secondary",            // mode  
   null,                   // tag set  
   { enabled: true }       // hedge options  
)  
db.collection.find({ }).readPref(  
   "secondary",                      // mode  
   [ { "datacenter": "B" },  { } ],  // tag set  
   { enabled: true }                 // hedge options  
)