<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>read_preferences – Utilities for choosing which member of a replica set to read from. — PyMongo 3.7.2 documentation</title> <link rel="stylesheet" href="../../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <script type="text/javascript" src="../../_static/language_data.js"></script> <script type="text/javascript" src="../../_static/sidebar.js"></script> <link rel="index" title="Index" href="../../genindex.html" /> <link rel="search" title="Search" href="../../search.html" /> <link rel="next" title="results – Result class definitions" href="results.html" /> <link rel="prev" title="read_concern – Tools for working with read concern." href="read_concern.html" /> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="results.html" title="results – Result class definitions" accesskey="N">next</a> |</li> <li class="right" > <a href="read_concern.html" title="read_concern – Tools for working with read concern." accesskey="P">previous</a> |</li> <li class="nav-item nav-item-0"><a href="../../index.html">PyMongo 3.7.2 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="../index.html" >API Documentation</a> »</li> <li class="nav-item nav-item-2"><a href="index.html" accesskey="U"><code class="docutils literal notranslate"><span class="pre">pymongo</span></code> – Python driver for MongoDB</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-pymongo.read_preferences"> <span id="read-preferences-utilities-for-choosing-which-member-of-a-replica-set-to-read-from"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">read_preferences</span></code> – Utilities for choosing which member of a replica set to read from.<a class="headerlink" href="#module-pymongo.read_preferences" title="Permalink to this headline">¶</a></h1> <p>Utilities for choosing which member of a replica set to read from.</p> <dl class="class"> <dt id="pymongo.read_preferences.Primary"> <em class="property">class </em><code class="descclassname">pymongo.read_preferences.</code><code class="descname">Primary</code><a class="headerlink" href="#pymongo.read_preferences.Primary" title="Permalink to this definition">¶</a></dt> <dd><p>Primary read preference.</p> <ul class="simple"> <li>When directly connected to one mongod queries are allowed if the server is standalone or a replica set primary.</li> <li>When connected to a mongos queries are sent to the primary of a shard.</li> <li>When connected to a replica set queries are sent to the primary of the replica set.</li> </ul> <dl class="attribute"> <dt id="pymongo.read_preferences.Primary.document"> <code class="descname">document</code><a class="headerlink" href="#pymongo.read_preferences.Primary.document" title="Permalink to this definition">¶</a></dt> <dd><p>Read preference as a document.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Primary.mode"> <code class="descname">mode</code><a class="headerlink" href="#pymongo.read_preferences.Primary.mode" title="Permalink to this definition">¶</a></dt> <dd><p>The mode of this read preference instance.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Primary.name"> <code class="descname">name</code><a class="headerlink" href="#pymongo.read_preferences.Primary.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of this read preference.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="pymongo.read_preferences.PrimaryPreferred"> <em class="property">class </em><code class="descclassname">pymongo.read_preferences.</code><code class="descname">PrimaryPreferred</code><span class="sig-paren">(</span><em>tag_sets=None</em>, <em>max_staleness=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.read_preferences.PrimaryPreferred" title="Permalink to this definition">¶</a></dt> <dd><p>PrimaryPreferred read preference.</p> <ul class="simple"> <li>When directly connected to one mongod queries are allowed to standalone servers, to a replica set primary, or to replica set secondaries.</li> <li>When connected to a mongos queries are sent to the primary of a shard if available, otherwise a shard secondary.</li> <li>When connected to a replica set queries are sent to the primary if available, otherwise a secondary.</li> </ul> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <li><cite>tag_sets</cite>: The <a class="reference internal" href="#pymongo.read_preferences.PrimaryPreferred.tag_sets" title="pymongo.read_preferences.PrimaryPreferred.tag_sets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tag_sets</span></code></a> to use if the primary is not available.</li> <li><cite>max_staleness</cite>: (integer, in seconds) The maximum estimated length of time a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations. Default -1, meaning no maximum. If it is set, it must be at least 90 seconds.</li> </ul> </td> </tr> </tbody> </table> <dl class="attribute"> <dt id="pymongo.read_preferences.PrimaryPreferred.document"> <code class="descname">document</code><a class="headerlink" href="#pymongo.read_preferences.PrimaryPreferred.document" title="Permalink to this definition">¶</a></dt> <dd><p>Read preference as a document.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.PrimaryPreferred.max_staleness"> <code class="descname">max_staleness</code><a class="headerlink" href="#pymongo.read_preferences.PrimaryPreferred.max_staleness" title="Permalink to this definition">¶</a></dt> <dd><p>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.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.PrimaryPreferred.min_wire_version"> <code class="descname">min_wire_version</code><a class="headerlink" href="#pymongo.read_preferences.PrimaryPreferred.min_wire_version" title="Permalink to this definition">¶</a></dt> <dd><p>The wire protocol version the server must support.</p> <p>Some read preferences impose version requirements on all servers (e.g. maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).</p> <p>All servers’ maxWireVersion must be at least this read preference’s <cite>min_wire_version</cite>, or the driver raises <a class="reference internal" href="errors.html#pymongo.errors.ConfigurationError" title="pymongo.errors.ConfigurationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConfigurationError</span></code></a>.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.PrimaryPreferred.mode"> <code class="descname">mode</code><a class="headerlink" href="#pymongo.read_preferences.PrimaryPreferred.mode" title="Permalink to this definition">¶</a></dt> <dd><p>The mode of this read preference instance.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.PrimaryPreferred.mongos_mode"> <code class="descname">mongos_mode</code><a class="headerlink" href="#pymongo.read_preferences.PrimaryPreferred.mongos_mode" title="Permalink to this definition">¶</a></dt> <dd><p>The mongos mode of this read preference.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.PrimaryPreferred.name"> <code class="descname">name</code><a class="headerlink" href="#pymongo.read_preferences.PrimaryPreferred.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of this read preference.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.PrimaryPreferred.tag_sets"> <code class="descname">tag_sets</code><a class="headerlink" href="#pymongo.read_preferences.PrimaryPreferred.tag_sets" title="Permalink to this definition">¶</a></dt> <dd><p>Set <code class="docutils literal notranslate"><span class="pre">tag_sets</span></code> to a list of dictionaries like [{‘dc’: ‘ny’}] to read only from members whose <code class="docutils literal notranslate"><span class="pre">dc</span></code> tag has the value <code class="docutils literal notranslate"><span class="pre">"ny"</span></code>. To specify a priority-order for tag sets, provide a list of tag sets: <code class="docutils literal notranslate"><span class="pre">[{'dc':</span> <span class="pre">'ny'},</span> <span class="pre">{'dc':</span> <span class="pre">'la'},</span> <span class="pre">{}]</span></code>. A final, empty tag set, <code class="docutils literal notranslate"><span class="pre">{}</span></code>, means “read from any member that matches the mode, ignoring tags.” MongoReplicaSetClient tries each set of tags in turn until it finds a set of tags with at least one matching member.</p> <blockquote> <div><div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference external" href="http://www.mongodb.org/display/DOCS/Data+Center+Awareness">Data-Center Awareness</a></p> </div> </div></blockquote> </dd></dl> </dd></dl> <dl class="class"> <dt id="pymongo.read_preferences.Secondary"> <em class="property">class </em><code class="descclassname">pymongo.read_preferences.</code><code class="descname">Secondary</code><span class="sig-paren">(</span><em>tag_sets=None</em>, <em>max_staleness=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.read_preferences.Secondary" title="Permalink to this definition">¶</a></dt> <dd><p>Secondary read preference.</p> <ul class="simple"> <li>When directly connected to one mongod queries are allowed to standalone servers, to a replica set primary, or to replica set secondaries.</li> <li>When connected to a mongos queries are distributed among shard secondaries. An error is raised if no secondaries are available.</li> <li>When connected to a replica set queries are distributed among secondaries. An error is raised if no secondaries are available.</li> </ul> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <li><cite>tag_sets</cite>: The <a class="reference internal" href="#pymongo.read_preferences.Secondary.tag_sets" title="pymongo.read_preferences.Secondary.tag_sets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tag_sets</span></code></a> for this read preference.</li> <li><cite>max_staleness</cite>: (integer, in seconds) The maximum estimated length of time a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations. Default -1, meaning no maximum. If it is set, it must be at least 90 seconds.</li> </ul> </td> </tr> </tbody> </table> <dl class="attribute"> <dt id="pymongo.read_preferences.Secondary.document"> <code class="descname">document</code><a class="headerlink" href="#pymongo.read_preferences.Secondary.document" title="Permalink to this definition">¶</a></dt> <dd><p>Read preference as a document.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Secondary.max_staleness"> <code class="descname">max_staleness</code><a class="headerlink" href="#pymongo.read_preferences.Secondary.max_staleness" title="Permalink to this definition">¶</a></dt> <dd><p>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.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Secondary.min_wire_version"> <code class="descname">min_wire_version</code><a class="headerlink" href="#pymongo.read_preferences.Secondary.min_wire_version" title="Permalink to this definition">¶</a></dt> <dd><p>The wire protocol version the server must support.</p> <p>Some read preferences impose version requirements on all servers (e.g. maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).</p> <p>All servers’ maxWireVersion must be at least this read preference’s <cite>min_wire_version</cite>, or the driver raises <a class="reference internal" href="errors.html#pymongo.errors.ConfigurationError" title="pymongo.errors.ConfigurationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConfigurationError</span></code></a>.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Secondary.mode"> <code class="descname">mode</code><a class="headerlink" href="#pymongo.read_preferences.Secondary.mode" title="Permalink to this definition">¶</a></dt> <dd><p>The mode of this read preference instance.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Secondary.mongos_mode"> <code class="descname">mongos_mode</code><a class="headerlink" href="#pymongo.read_preferences.Secondary.mongos_mode" title="Permalink to this definition">¶</a></dt> <dd><p>The mongos mode of this read preference.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Secondary.name"> <code class="descname">name</code><a class="headerlink" href="#pymongo.read_preferences.Secondary.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of this read preference.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Secondary.tag_sets"> <code class="descname">tag_sets</code><a class="headerlink" href="#pymongo.read_preferences.Secondary.tag_sets" title="Permalink to this definition">¶</a></dt> <dd><p>Set <code class="docutils literal notranslate"><span class="pre">tag_sets</span></code> to a list of dictionaries like [{‘dc’: ‘ny’}] to read only from members whose <code class="docutils literal notranslate"><span class="pre">dc</span></code> tag has the value <code class="docutils literal notranslate"><span class="pre">"ny"</span></code>. To specify a priority-order for tag sets, provide a list of tag sets: <code class="docutils literal notranslate"><span class="pre">[{'dc':</span> <span class="pre">'ny'},</span> <span class="pre">{'dc':</span> <span class="pre">'la'},</span> <span class="pre">{}]</span></code>. A final, empty tag set, <code class="docutils literal notranslate"><span class="pre">{}</span></code>, means “read from any member that matches the mode, ignoring tags.” MongoReplicaSetClient tries each set of tags in turn until it finds a set of tags with at least one matching member.</p> <blockquote> <div><div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference external" href="http://www.mongodb.org/display/DOCS/Data+Center+Awareness">Data-Center Awareness</a></p> </div> </div></blockquote> </dd></dl> </dd></dl> <dl class="class"> <dt id="pymongo.read_preferences.SecondaryPreferred"> <em class="property">class </em><code class="descclassname">pymongo.read_preferences.</code><code class="descname">SecondaryPreferred</code><span class="sig-paren">(</span><em>tag_sets=None</em>, <em>max_staleness=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.read_preferences.SecondaryPreferred" title="Permalink to this definition">¶</a></dt> <dd><p>SecondaryPreferred read preference.</p> <ul class="simple"> <li>When directly connected to one mongod queries are allowed to standalone servers, to a replica set primary, or to replica set secondaries.</li> <li>When connected to a mongos queries are distributed among shard secondaries, or the shard primary if no secondary is available.</li> <li>When connected to a replica set queries are distributed among secondaries, or the primary if no secondary is available.</li> </ul> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <li><cite>tag_sets</cite>: The <a class="reference internal" href="#pymongo.read_preferences.SecondaryPreferred.tag_sets" title="pymongo.read_preferences.SecondaryPreferred.tag_sets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tag_sets</span></code></a> for this read preference.</li> <li><cite>max_staleness</cite>: (integer, in seconds) The maximum estimated length of time a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations. Default -1, meaning no maximum. If it is set, it must be at least 90 seconds.</li> </ul> </td> </tr> </tbody> </table> <dl class="attribute"> <dt id="pymongo.read_preferences.SecondaryPreferred.document"> <code class="descname">document</code><a class="headerlink" href="#pymongo.read_preferences.SecondaryPreferred.document" title="Permalink to this definition">¶</a></dt> <dd><p>Read preference as a document.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.SecondaryPreferred.max_staleness"> <code class="descname">max_staleness</code><a class="headerlink" href="#pymongo.read_preferences.SecondaryPreferred.max_staleness" title="Permalink to this definition">¶</a></dt> <dd><p>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.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.SecondaryPreferred.min_wire_version"> <code class="descname">min_wire_version</code><a class="headerlink" href="#pymongo.read_preferences.SecondaryPreferred.min_wire_version" title="Permalink to this definition">¶</a></dt> <dd><p>The wire protocol version the server must support.</p> <p>Some read preferences impose version requirements on all servers (e.g. maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).</p> <p>All servers’ maxWireVersion must be at least this read preference’s <cite>min_wire_version</cite>, or the driver raises <a class="reference internal" href="errors.html#pymongo.errors.ConfigurationError" title="pymongo.errors.ConfigurationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConfigurationError</span></code></a>.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.SecondaryPreferred.mode"> <code class="descname">mode</code><a class="headerlink" href="#pymongo.read_preferences.SecondaryPreferred.mode" title="Permalink to this definition">¶</a></dt> <dd><p>The mode of this read preference instance.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.SecondaryPreferred.mongos_mode"> <code class="descname">mongos_mode</code><a class="headerlink" href="#pymongo.read_preferences.SecondaryPreferred.mongos_mode" title="Permalink to this definition">¶</a></dt> <dd><p>The mongos mode of this read preference.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.SecondaryPreferred.name"> <code class="descname">name</code><a class="headerlink" href="#pymongo.read_preferences.SecondaryPreferred.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of this read preference.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.SecondaryPreferred.tag_sets"> <code class="descname">tag_sets</code><a class="headerlink" href="#pymongo.read_preferences.SecondaryPreferred.tag_sets" title="Permalink to this definition">¶</a></dt> <dd><p>Set <code class="docutils literal notranslate"><span class="pre">tag_sets</span></code> to a list of dictionaries like [{‘dc’: ‘ny’}] to read only from members whose <code class="docutils literal notranslate"><span class="pre">dc</span></code> tag has the value <code class="docutils literal notranslate"><span class="pre">"ny"</span></code>. To specify a priority-order for tag sets, provide a list of tag sets: <code class="docutils literal notranslate"><span class="pre">[{'dc':</span> <span class="pre">'ny'},</span> <span class="pre">{'dc':</span> <span class="pre">'la'},</span> <span class="pre">{}]</span></code>. A final, empty tag set, <code class="docutils literal notranslate"><span class="pre">{}</span></code>, means “read from any member that matches the mode, ignoring tags.” MongoReplicaSetClient tries each set of tags in turn until it finds a set of tags with at least one matching member.</p> <blockquote> <div><div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference external" href="http://www.mongodb.org/display/DOCS/Data+Center+Awareness">Data-Center Awareness</a></p> </div> </div></blockquote> </dd></dl> </dd></dl> <dl class="class"> <dt id="pymongo.read_preferences.Nearest"> <em class="property">class </em><code class="descclassname">pymongo.read_preferences.</code><code class="descname">Nearest</code><span class="sig-paren">(</span><em>tag_sets=None</em>, <em>max_staleness=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.read_preferences.Nearest" title="Permalink to this definition">¶</a></dt> <dd><p>Nearest read preference.</p> <ul class="simple"> <li>When directly connected to one mongod queries are allowed to standalone servers, to a replica set primary, or to replica set secondaries.</li> <li>When connected to a mongos queries are distributed among all members of a shard.</li> <li>When connected to a replica set queries are distributed among all members.</li> </ul> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <li><cite>tag_sets</cite>: The <a class="reference internal" href="#pymongo.read_preferences.Nearest.tag_sets" title="pymongo.read_preferences.Nearest.tag_sets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tag_sets</span></code></a> for this read preference.</li> <li><cite>max_staleness</cite>: (integer, in seconds) The maximum estimated length of time a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations. Default -1, meaning no maximum. If it is set, it must be at least 90 seconds.</li> </ul> </td> </tr> </tbody> </table> <dl class="attribute"> <dt id="pymongo.read_preferences.Nearest.document"> <code class="descname">document</code><a class="headerlink" href="#pymongo.read_preferences.Nearest.document" title="Permalink to this definition">¶</a></dt> <dd><p>Read preference as a document.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Nearest.max_staleness"> <code class="descname">max_staleness</code><a class="headerlink" href="#pymongo.read_preferences.Nearest.max_staleness" title="Permalink to this definition">¶</a></dt> <dd><p>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.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Nearest.min_wire_version"> <code class="descname">min_wire_version</code><a class="headerlink" href="#pymongo.read_preferences.Nearest.min_wire_version" title="Permalink to this definition">¶</a></dt> <dd><p>The wire protocol version the server must support.</p> <p>Some read preferences impose version requirements on all servers (e.g. maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).</p> <p>All servers’ maxWireVersion must be at least this read preference’s <cite>min_wire_version</cite>, or the driver raises <a class="reference internal" href="errors.html#pymongo.errors.ConfigurationError" title="pymongo.errors.ConfigurationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConfigurationError</span></code></a>.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Nearest.mode"> <code class="descname">mode</code><a class="headerlink" href="#pymongo.read_preferences.Nearest.mode" title="Permalink to this definition">¶</a></dt> <dd><p>The mode of this read preference instance.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Nearest.mongos_mode"> <code class="descname">mongos_mode</code><a class="headerlink" href="#pymongo.read_preferences.Nearest.mongos_mode" title="Permalink to this definition">¶</a></dt> <dd><p>The mongos mode of this read preference.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Nearest.name"> <code class="descname">name</code><a class="headerlink" href="#pymongo.read_preferences.Nearest.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of this read preference.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.Nearest.tag_sets"> <code class="descname">tag_sets</code><a class="headerlink" href="#pymongo.read_preferences.Nearest.tag_sets" title="Permalink to this definition">¶</a></dt> <dd><p>Set <code class="docutils literal notranslate"><span class="pre">tag_sets</span></code> to a list of dictionaries like [{‘dc’: ‘ny’}] to read only from members whose <code class="docutils literal notranslate"><span class="pre">dc</span></code> tag has the value <code class="docutils literal notranslate"><span class="pre">"ny"</span></code>. To specify a priority-order for tag sets, provide a list of tag sets: <code class="docutils literal notranslate"><span class="pre">[{'dc':</span> <span class="pre">'ny'},</span> <span class="pre">{'dc':</span> <span class="pre">'la'},</span> <span class="pre">{}]</span></code>. A final, empty tag set, <code class="docutils literal notranslate"><span class="pre">{}</span></code>, means “read from any member that matches the mode, ignoring tags.” MongoReplicaSetClient tries each set of tags in turn until it finds a set of tags with at least one matching member.</p> <blockquote> <div><div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference external" href="http://www.mongodb.org/display/DOCS/Data+Center+Awareness">Data-Center Awareness</a></p> </div> </div></blockquote> </dd></dl> </dd></dl> <dl class="class"> <dt id="pymongo.read_preferences.ReadPreference"> <em class="property">class </em><code class="descclassname">pymongo.read_preferences.</code><code class="descname">ReadPreference</code><a class="headerlink" href="#pymongo.read_preferences.ReadPreference" title="Permalink to this definition">¶</a></dt> <dd><p>An enum that defines the read preference modes supported by PyMongo.</p> <p>See <a class="reference internal" href="../../examples/high_availability.html"><span class="doc">High Availability and PyMongo</span></a> for code examples.</p> <p>A read preference is used in three cases:</p> <p><a class="reference internal" href="mongo_client.html#pymongo.mongo_client.MongoClient" title="pymongo.mongo_client.MongoClient"><code class="xref py py-class docutils literal notranslate"><span class="pre">MongoClient</span></code></a> connected to a single mongod:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PRIMARY</span></code>: Queries are allowed if the server is standalone or a replica set primary.</li> <li>All other modes allow queries to standalone servers, to a replica set primary, or to replica set secondaries.</li> </ul> <p><a class="reference internal" href="mongo_client.html#pymongo.mongo_client.MongoClient" title="pymongo.mongo_client.MongoClient"><code class="xref py py-class docutils literal notranslate"><span class="pre">MongoClient</span></code></a> initialized with the <code class="docutils literal notranslate"><span class="pre">replicaSet</span></code> option:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PRIMARY</span></code>: Read from the primary. This is the default, and provides the strongest consistency. If no primary is available, raise <a class="reference internal" href="errors.html#pymongo.errors.AutoReconnect" title="pymongo.errors.AutoReconnect"><code class="xref py py-class docutils literal notranslate"><span class="pre">AutoReconnect</span></code></a>.</li> <li><code class="docutils literal notranslate"><span class="pre">PRIMARY_PREFERRED</span></code>: Read from the primary if available, or if there is none, read from a secondary.</li> <li><code class="docutils literal notranslate"><span class="pre">SECONDARY</span></code>: Read from a secondary. If no secondary is available, raise <a class="reference internal" href="errors.html#pymongo.errors.AutoReconnect" title="pymongo.errors.AutoReconnect"><code class="xref py py-class docutils literal notranslate"><span class="pre">AutoReconnect</span></code></a>.</li> <li><code class="docutils literal notranslate"><span class="pre">SECONDARY_PREFERRED</span></code>: Read from a secondary if available, otherwise from the primary.</li> <li><code class="docutils literal notranslate"><span class="pre">NEAREST</span></code>: Read from any member.</li> </ul> <p><a class="reference internal" href="mongo_client.html#pymongo.mongo_client.MongoClient" title="pymongo.mongo_client.MongoClient"><code class="xref py py-class docutils literal notranslate"><span class="pre">MongoClient</span></code></a> connected to a mongos, with a sharded cluster of replica sets:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PRIMARY</span></code>: Read from the primary of the shard, or raise <a class="reference internal" href="errors.html#pymongo.errors.OperationFailure" title="pymongo.errors.OperationFailure"><code class="xref py py-class docutils literal notranslate"><span class="pre">OperationFailure</span></code></a> if there is none. This is the default.</li> <li><code class="docutils literal notranslate"><span class="pre">PRIMARY_PREFERRED</span></code>: Read from the primary of the shard, or if there is none, read from a secondary of the shard.</li> <li><code class="docutils literal notranslate"><span class="pre">SECONDARY</span></code>: Read from a secondary of the shard, or raise <a class="reference internal" href="errors.html#pymongo.errors.OperationFailure" title="pymongo.errors.OperationFailure"><code class="xref py py-class docutils literal notranslate"><span class="pre">OperationFailure</span></code></a> if there is none.</li> <li><code class="docutils literal notranslate"><span class="pre">SECONDARY_PREFERRED</span></code>: Read from a secondary of the shard if available, otherwise from the shard primary.</li> <li><code class="docutils literal notranslate"><span class="pre">NEAREST</span></code>: Read from any shard member.</li> </ul> <dl class="attribute"> <dt id="pymongo.read_preferences.ReadPreference.PRIMARY"> <code class="descname">PRIMARY</code><em class="property"> = Primary()</em><a class="headerlink" href="#pymongo.read_preferences.ReadPreference.PRIMARY" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED"> <code class="descname">PRIMARY_PREFERRED</code><em class="property"> = PrimaryPreferred(tag_sets=None, max_staleness=-1)</em><a class="headerlink" href="#pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.ReadPreference.SECONDARY"> <code class="descname">SECONDARY</code><em class="property"> = Secondary(tag_sets=None, max_staleness=-1)</em><a class="headerlink" href="#pymongo.read_preferences.ReadPreference.SECONDARY" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.ReadPreference.SECONDARY_PREFERRED"> <code class="descname">SECONDARY_PREFERRED</code><em class="property"> = SecondaryPreferred(tag_sets=None, max_staleness=-1)</em><a class="headerlink" href="#pymongo.read_preferences.ReadPreference.SECONDARY_PREFERRED" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="attribute"> <dt id="pymongo.read_preferences.ReadPreference.NEAREST"> <code class="descname">NEAREST</code><em class="property"> = Nearest(tag_sets=None, max_staleness=-1)</em><a class="headerlink" href="#pymongo.read_preferences.ReadPreference.NEAREST" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> </dd></dl> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="read_concern.html" title="previous chapter"><code class="docutils literal notranslate"><span class="pre">read_concern</span></code> – Tools for working with read concern.</a></p> <h4>Next topic</h4> <p class="topless"><a href="results.html" title="next chapter"><code class="docutils literal notranslate"><span class="pre">results</span></code> – Result class definitions</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/api/pymongo/read_preferences.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="results.html" title="results – Result class definitions" >next</a> |</li> <li class="right" > <a href="read_concern.html" title="read_concern – Tools for working with read concern." >previous</a> |</li> <li class="nav-item nav-item-0"><a href="../../index.html">PyMongo 3.7.2 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="../index.html" >API Documentation</a> »</li> <li class="nav-item nav-item-2"><a href="index.html" ><code class="docutils literal notranslate"><span class="pre">pymongo</span></code> – Python driver for MongoDB</a> »</li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright MongoDB, Inc. 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc. </div> </body> </html>