Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-release > by-pkgid > bc55833f04f370ac3ed453ef5b0ad686 > files > 289

python2-gridfs-3.7.2-1.mga7.i586.rpm


<!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>Frequently Asked Questions &#8212; 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="Compatibility Policy" href="compatibility-policy.html" />
    <link rel="prev" title="TLS/SSL and PyMongo" href="examples/tls.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="compatibility-policy.html" title="Compatibility Policy"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="examples/tls.html" title="TLS/SSL and PyMongo"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyMongo 3.7.2 documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="frequently-asked-questions">
<h1><a class="toc-backref" href="#id1">Frequently Asked Questions</a><a class="headerlink" href="#frequently-asked-questions" title="Permalink to this headline">¶</a></h1>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#frequently-asked-questions" id="id1">Frequently Asked Questions</a><ul>
<li><a class="reference internal" href="#is-pymongo-thread-safe" id="id2">Is PyMongo thread-safe?</a></li>
<li><a class="reference internal" href="#is-pymongo-fork-safe" id="id3">Is PyMongo fork-safe?</a></li>
<li><a class="reference internal" href="#how-does-connection-pooling-work-in-pymongo" id="id4">How does connection pooling work in PyMongo?</a></li>
<li><a class="reference internal" href="#does-pymongo-support-python-3" id="id5">Does PyMongo support Python 3?</a></li>
<li><a class="reference internal" href="#does-pymongo-support-asynchronous-frameworks-like-gevent-asyncio-tornado-or-twisted" id="id6">Does PyMongo support asynchronous frameworks like Gevent, asyncio, Tornado, or Twisted?</a></li>
<li><a class="reference internal" href="#why-does-pymongo-add-an-id-field-to-all-of-my-documents" id="id7">Why does PyMongo add an _id field to all of my documents?</a></li>
<li><a class="reference internal" href="#key-order-in-subdocuments-why-does-my-query-work-in-the-shell-but-not-pymongo" id="id8">Key order in subdocuments – why does my query work in the shell but not PyMongo?</a></li>
<li><a class="reference internal" href="#what-does-cursornotfound-cursor-id-not-valid-at-server-mean" id="id9">What does <em>CursorNotFound</em> cursor id not valid at server mean?</a></li>
<li><a class="reference internal" href="#how-do-i-change-the-timeout-value-for-cursors" id="id10">How do I change the timeout value for cursors?</a></li>
<li><a class="reference internal" href="#how-can-i-store-decimal-decimal-instances" id="id11">How can I store <code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal.Decimal</span></code> instances?</a></li>
<li><a class="reference internal" href="#i-m-saving-9-99-but-when-i-query-my-document-contains-9-9900000000000002-what-s-going-on-here" id="id12">I’m saving <code class="docutils literal notranslate"><span class="pre">9.99</span></code> but when I query my document contains <code class="docutils literal notranslate"><span class="pre">9.9900000000000002</span></code> - what’s going on here?</a></li>
<li><a class="reference internal" href="#can-you-add-attribute-style-access-for-documents" id="id13">Can you add attribute style access for documents?</a></li>
<li><a class="reference internal" href="#what-is-the-correct-way-to-handle-time-zones-with-pymongo" id="id14">What is the correct way to handle time zones with PyMongo?</a></li>
<li><a class="reference internal" href="#how-can-i-save-a-datetime-date-instance" id="id15">How can I save a <code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime.date</span></code> instance?</a></li>
<li><a class="reference internal" href="#when-i-query-for-a-document-by-objectid-in-my-web-application-i-get-no-result" id="id16">When I query for a document by ObjectId in my web application I get no result</a></li>
<li><a class="reference internal" href="#how-can-i-use-pymongo-from-django" id="id17">How can I use PyMongo from Django?</a></li>
<li><a class="reference internal" href="#does-pymongo-work-with-mod-wsgi" id="id18">Does PyMongo work with <strong>mod_wsgi</strong>?</a></li>
<li><a class="reference internal" href="#does-pymongo-work-with-pythonanywhere" id="id19">Does PyMongo work with PythonAnywhere?</a></li>
<li><a class="reference internal" href="#how-can-i-use-something-like-python-s-json-module-to-encode-my-documents-to-json" id="id20">How can I use something like Python’s <code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> module to encode my documents to JSON?</a></li>
<li><a class="reference internal" href="#why-do-i-get-overflowerror-decoding-dates-stored-by-another-language-s-driver" id="id21">Why do I get OverflowError decoding dates stored by another language’s driver?</a></li>
<li><a class="reference internal" href="#using-pymongo-with-multiprocessing" id="id22">Using PyMongo with Multiprocessing</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="is-pymongo-thread-safe">
<h2><a class="toc-backref" href="#id2">Is PyMongo thread-safe?</a><a class="headerlink" href="#is-pymongo-thread-safe" title="Permalink to this headline">¶</a></h2>
<p>PyMongo is thread-safe and provides built-in connection pooling
for threaded applications.</p>
</div>
<div class="section" id="is-pymongo-fork-safe">
<span id="pymongo-fork-safe"></span><h2><a class="toc-backref" href="#id3">Is PyMongo fork-safe?</a><a class="headerlink" href="#is-pymongo-fork-safe" title="Permalink to this headline">¶</a></h2>
<p>PyMongo is not fork-safe. Care must be taken when using instances of
<a class="reference internal" href="api/pymongo/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> with <code class="docutils literal notranslate"><span class="pre">fork()</span></code>. Specifically,
instances of MongoClient must not be copied from a parent process to
a child process. Instead, the parent process and each child process must
create their own instances of MongoClient. Instances of MongoClient copied from
the parent process have a high probability of deadlock in the child process due
to the inherent incompatibilities between <code class="docutils literal notranslate"><span class="pre">fork()</span></code>, threads, and locks
described <a class="reference internal" href="#pymongo-fork-safe-details"><span class="std std-ref">below</span></a>. PyMongo will attempt to
issue a warning if there is a chance of this deadlock occurring.</p>
<p id="pymongo-fork-safe-details">MongoClient spawns multiple threads to run background tasks such as monitoring
connected servers. These threads share state that is protected by instances of
<code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code>, which are themselves <a class="reference external" href="http://bugs.python.org/issue6721">not fork-safe</a>. The
driver is therefore subject to the same limitations as any other multithreaded
code that uses <code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code> (and mutexes in general). One of these
limitations is that the locks become useless after <code class="docutils literal notranslate"><span class="pre">fork()</span></code>. During the fork,
all locks are copied over to the child process in the same state as they were
in the parent: if they were locked, the copied locks are also locked. The child
created by <code class="docutils literal notranslate"><span class="pre">fork()</span></code> only has one thread, so any locks that were taken out by
other threads in the parent will never be released in the child. The next time
the child process attempts to acquire one of these locks, deadlock occurs.</p>
<p>For a long but interesting read about the problems of Python locks in
multithreaded contexts with <code class="docutils literal notranslate"><span class="pre">fork()</span></code>, see <a class="reference external" href="http://bugs.python.org/issue6721">http://bugs.python.org/issue6721</a>.</p>
</div>
<div class="section" id="how-does-connection-pooling-work-in-pymongo">
<span id="connection-pooling"></span><h2><a class="toc-backref" href="#id4">How does connection pooling work in PyMongo?</a><a class="headerlink" href="#how-does-connection-pooling-work-in-pymongo" title="Permalink to this headline">¶</a></h2>
<p>Every <a class="reference internal" href="api/pymongo/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> instance has a built-in
connection pool per server in your MongoDB topology. These pools open sockets
on demand to support the number of concurrent MongoDB operations that your
multi-threaded application requires. There is no thread-affinity for sockets.</p>
<p>The size of each connection pool is capped at <code class="docutils literal notranslate"><span class="pre">maxPoolSize</span></code>, which defaults
to 100. If there are <code class="docutils literal notranslate"><span class="pre">maxPoolSize</span></code> connections to a server and all are in
use, the next request to that server will wait until one of the connections
becomes available.</p>
<p>The client instance opens one additional socket per server in your MongoDB
topology for monitoring the server’s state.</p>
<p>For example, a client connected to a 3-node replica set opens 3 monitoring
sockets. It also opens as many sockets as needed to support a multi-threaded
application’s concurrent operations on each server, up to <code class="docutils literal notranslate"><span class="pre">maxPoolSize</span></code>. With
a <code class="docutils literal notranslate"><span class="pre">maxPoolSize</span></code> of 100, if the application only uses the primary (the
default), then only the primary connection pool grows and the total connections
is at most 103. If the application uses a
<a class="reference internal" href="api/pymongo/read_preferences.html#pymongo.read_preferences.ReadPreference" title="pymongo.read_preferences.ReadPreference"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadPreference</span></code></a> to query the secondaries,
their pools also grow and the total connections can reach 303.</p>
<p>It is possible to set the minimum number of concurrent connections to each
server with <code class="docutils literal notranslate"><span class="pre">minPoolSize</span></code>, which defaults to 0. The connection pool will be
initialized with this number of sockets. If sockets are closed due to any
network errors, causing the total number of sockets (both in use and idle) to
drop below the minimum, more sockets are opened until the minimum is reached.</p>
<p>The maximum number of milliseconds that a connection can remain idle in the
pool before being removed and replaced can be set with <code class="docutils literal notranslate"><span class="pre">maxIdleTime</span></code>, which
defaults to <cite>None</cite> (no limit).</p>
<p>The default configuration for a <a class="reference internal" href="api/pymongo/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>
works for most applications:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">)</span>
</pre></div>
</div>
<p>Create this client <strong>once</strong> for each process, and reuse it for all
operations. It is a common mistake to create a new client for each request,
which is very inefficient.</p>
<p>To support extremely high numbers of concurrent MongoDB operations within one
process, increase <code class="docutils literal notranslate"><span class="pre">maxPoolSize</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">maxPoolSize</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
</pre></div>
</div>
<p>… or make it unbounded:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">maxPoolSize</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</pre></div>
</div>
<p>By default, any number of threads are allowed to wait for sockets to become
available, and they can wait any length of time. Override <code class="docutils literal notranslate"><span class="pre">waitQueueMultiple</span></code>
to cap the number of waiting threads. E.g., to keep the number of waiters less
than or equal to 500:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">maxPoolSize</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">waitQueueMultiple</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<p>When 500 threads are waiting for a socket, the 501st that needs a socket
raises <a class="reference internal" href="api/pymongo/errors.html#pymongo.errors.ExceededMaxWaiters" title="pymongo.errors.ExceededMaxWaiters"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExceededMaxWaiters</span></code></a>. Use this option to
bound the amount of queueing in your application during a load spike, at the
cost of additional exceptions.</p>
<p>Once the pool reaches its max size, additional threads are allowed to wait
indefinitely for sockets to become available, unless you set
<code class="docutils literal notranslate"><span class="pre">waitQueueTimeoutMS</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">waitQueueTimeoutMS</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>A thread that waits more than 100ms (in this example) for a socket raises
<a class="reference internal" href="api/pymongo/errors.html#pymongo.errors.ConnectionFailure" title="pymongo.errors.ConnectionFailure"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionFailure</span></code></a>. Use this option if it is more
important to bound the duration of operations during a load spike than it is to
complete every operation.</p>
<p>When <a class="reference internal" href="api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient.close" title="pymongo.mongo_client.MongoClient.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> is called by any thread,
all idle sockets are closed, and all sockets that are in use will be closed as
they are returned to the pool.</p>
</div>
<div class="section" id="does-pymongo-support-python-3">
<h2><a class="toc-backref" href="#id5">Does PyMongo support Python 3?</a><a class="headerlink" href="#does-pymongo-support-python-3" title="Permalink to this headline">¶</a></h2>
<p>PyMongo supports CPython 3.4+ and PyPy3. See the <a class="reference internal" href="python3.html"><span class="doc">Python 3 FAQ</span></a> for details.</p>
</div>
<div class="section" id="does-pymongo-support-asynchronous-frameworks-like-gevent-asyncio-tornado-or-twisted">
<h2><a class="toc-backref" href="#id6">Does PyMongo support asynchronous frameworks like Gevent, asyncio, Tornado, or Twisted?</a><a class="headerlink" href="#does-pymongo-support-asynchronous-frameworks-like-gevent-asyncio-tornado-or-twisted" title="Permalink to this headline">¶</a></h2>
<p>PyMongo fully supports <a class="reference internal" href="examples/gevent.html"><span class="doc">Gevent</span></a>.</p>
<p>To use MongoDB with <a class="reference external" href="https://docs.python.org/3/library/asyncio.html">asyncio</a>
or <a class="reference external" href="http://www.tornadoweb.org/">Tornado</a>, see the
<a class="reference external" href="https://github.com/mongodb/motor">Motor</a> project.</p>
<p>For <a class="reference external" href="http://twistedmatrix.com/">Twisted</a>, see <a class="reference external" href="https://github.com/twisted/txmongo">TxMongo</a>. Its stated mission is to keep feature
parity with PyMongo.</p>
</div>
<div class="section" id="why-does-pymongo-add-an-id-field-to-all-of-my-documents">
<span id="writes-and-ids"></span><h2><a class="toc-backref" href="#id7">Why does PyMongo add an _id field to all of my documents?</a><a class="headerlink" href="#why-does-pymongo-add-an-id-field-to-all-of-my-documents" title="Permalink to this headline">¶</a></h2>
<p>When a document is inserted to MongoDB using
<a class="reference internal" href="api/pymongo/collection.html#pymongo.collection.Collection.insert_one" title="pymongo.collection.Collection.insert_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">insert_one()</span></code></a>,
<a class="reference internal" href="api/pymongo/collection.html#pymongo.collection.Collection.insert_many" title="pymongo.collection.Collection.insert_many"><code class="xref py py-meth docutils literal notranslate"><span class="pre">insert_many()</span></code></a>, or
<a class="reference internal" href="api/pymongo/collection.html#pymongo.collection.Collection.bulk_write" title="pymongo.collection.Collection.bulk_write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bulk_write()</span></code></a>, and that document does not
include an <code class="docutils literal notranslate"><span class="pre">_id</span></code> field, PyMongo automatically adds one for you, set to an
instance of <a class="reference internal" href="api/bson/objectid.html#bson.objectid.ObjectId" title="bson.objectid.ObjectId"><code class="xref py py-class docutils literal notranslate"><span class="pre">ObjectId</span></code></a>. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">my_doc</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collection</span><span class="o">.</span><span class="n">insert_one</span><span class="p">(</span><span class="n">my_doc</span><span class="p">)</span>
<span class="go">&lt;pymongo.results.InsertOneResult object at 0x7f3fc25bd640&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_doc</span>
<span class="go">{&#39;x&#39;: 1, &#39;_id&#39;: ObjectId(&#39;560db337fba522189f171720&#39;)}</span>
</pre></div>
</div>
<p>Users often discover this behavior when calling
<a class="reference internal" href="api/pymongo/collection.html#pymongo.collection.Collection.insert_many" title="pymongo.collection.Collection.insert_many"><code class="xref py py-meth docutils literal notranslate"><span class="pre">insert_many()</span></code></a> with a list of references
to a single document raises <a class="reference internal" href="api/pymongo/errors.html#pymongo.errors.BulkWriteError" title="pymongo.errors.BulkWriteError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BulkWriteError</span></code></a>. Several
Python idioms lead to this pitfall:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">doc</span> <span class="o">=</span> <span class="p">{}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collection</span><span class="o">.</span><span class="n">insert_many</span><span class="p">(</span><span class="n">doc</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">pymongo.errors.BulkWriteError</span>: <span class="n">batch op errors occurred</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">doc</span>
<span class="go">{&#39;_id&#39;: ObjectId(&#39;560f171cfba52279f0b0da0c&#39;)}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">docs</span> <span class="o">=</span> <span class="p">[{}]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collection</span><span class="o">.</span><span class="n">insert_many</span><span class="p">(</span><span class="n">docs</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">pymongo.errors.BulkWriteError</span>: <span class="n">batch op errors occurred</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">docs</span>
<span class="go">[{&#39;_id&#39;: ObjectId(&#39;560f1933fba52279f0b0da0e&#39;)}]</span>
</pre></div>
</div>
<p>PyMongo adds an <code class="docutils literal notranslate"><span class="pre">_id</span></code> field in this manner for a few reasons:</p>
<ul class="simple">
<li>All MongoDB documents are required to have an <code class="docutils literal notranslate"><span class="pre">_id</span></code> field.</li>
<li>If PyMongo were to insert a document without an <code class="docutils literal notranslate"><span class="pre">_id</span></code> MongoDB would add one
itself, but it would not report the value back to PyMongo.</li>
<li>Copying the document to insert before adding the <code class="docutils literal notranslate"><span class="pre">_id</span></code> field would be
prohibitively expensive for most high write volume applications.</li>
</ul>
<p>If you don’t want PyMongo to add an <code class="docutils literal notranslate"><span class="pre">_id</span></code> to your documents, insert only
documents that already have an <code class="docutils literal notranslate"><span class="pre">_id</span></code> field, added by your application.</p>
</div>
<div class="section" id="key-order-in-subdocuments-why-does-my-query-work-in-the-shell-but-not-pymongo">
<h2><a class="toc-backref" href="#id8">Key order in subdocuments – why does my query work in the shell but not PyMongo?</a><a class="headerlink" href="#key-order-in-subdocuments-why-does-my-query-work-in-the-shell-but-not-pymongo" title="Permalink to this headline">¶</a></h2>
<p>The key-value pairs in a BSON document can have any order (except that <code class="docutils literal notranslate"><span class="pre">_id</span></code>
is always first). The mongo shell preserves key order when reading and writing
data. Observe that “b” comes before “a” when we create the document and when it
is displayed:</p>
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="c1">// mongo shell.</span>
<span class="o">&gt;</span> <span class="nx">db</span><span class="p">.</span><span class="nx">collection</span><span class="p">.</span><span class="nx">insert</span><span class="p">(</span> <span class="p">{</span> <span class="s2">&quot;_id&quot;</span> <span class="o">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;subdocument&quot;</span> <span class="o">:</span> <span class="p">{</span> <span class="s2">&quot;b&quot;</span> <span class="o">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;a&quot;</span> <span class="o">:</span> <span class="mi">1</span> <span class="p">}</span> <span class="p">}</span> <span class="p">)</span>
<span class="nx">WriteResult</span><span class="p">({</span> <span class="s2">&quot;nInserted&quot;</span> <span class="o">:</span> <span class="mi">1</span> <span class="p">})</span>
<span class="o">&gt;</span> <span class="nx">db</span><span class="p">.</span><span class="nx">collection</span><span class="p">.</span><span class="nx">find</span><span class="p">()</span>
<span class="p">{</span> <span class="s2">&quot;_id&quot;</span> <span class="o">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;subdocument&quot;</span> <span class="o">:</span> <span class="p">{</span> <span class="s2">&quot;b&quot;</span> <span class="o">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;a&quot;</span> <span class="o">:</span> <span class="mi">1</span> <span class="p">}</span> <span class="p">}</span>
</pre></div>
</div>
<p>PyMongo represents BSON documents as Python dicts by default, and the order
of keys in dicts is not defined. That is, a dict declared with the “a” key
first is the same, to Python, as one with “b” first:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">({</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">})</span>
<span class="go">{&#39;a&#39;: 1.0, &#39;b&#39;: 1.0}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">({</span><span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">})</span>
<span class="go">{&#39;a&#39;: 1.0, &#39;b&#39;: 1.0}</span>
</pre></div>
</div>
<p>Therefore, Python dicts are not guaranteed to show keys in the order they are
stored in BSON. Here, “a” is shown before “b”:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">collection</span><span class="o">.</span><span class="n">find_one</span><span class="p">())</span>
<span class="go">{u&#39;_id&#39;: 1.0, u&#39;subdocument&#39;: {u&#39;a&#39;: 1.0, u&#39;b&#39;: 1.0}}</span>
</pre></div>
</div>
<p>To preserve order when reading BSON, use the <a class="reference internal" href="api/bson/son.html#bson.son.SON" title="bson.son.SON"><code class="xref py py-class docutils literal notranslate"><span class="pre">SON</span></code></a> class,
which is a dict that remembers its key order. First, get a handle to the
collection, configured to use <a class="reference internal" href="api/bson/son.html#bson.son.SON" title="bson.son.SON"><code class="xref py py-class docutils literal notranslate"><span class="pre">SON</span></code></a> instead of dict:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">bson</span> <span class="kn">import</span> <span class="n">CodecOptions</span><span class="p">,</span> <span class="n">SON</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">opts</span> <span class="o">=</span> <span class="n">CodecOptions</span><span class="p">(</span><span class="n">document_class</span><span class="o">=</span><span class="n">SON</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">opts</span>
<span class="go">CodecOptions(document_class=&lt;class &#39;bson.son.SON&#39;&gt;,</span>
<span class="go">             tz_aware=False,</span>
<span class="go">             uuid_representation=PYTHON_LEGACY,</span>
<span class="go">             unicode_decode_error_handler=&#39;strict&#39;,</span>
<span class="go">             tzinfo=None)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collection_son</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">with_options</span><span class="p">(</span><span class="n">codec_options</span><span class="o">=</span><span class="n">opts</span><span class="p">)</span>
</pre></div>
</div>
<p>Now, documents and subdocuments in query results are represented with
<a class="reference internal" href="api/bson/son.html#bson.son.SON" title="bson.son.SON"><code class="xref py py-class docutils literal notranslate"><span class="pre">SON</span></code></a> objects:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">collection_son</span><span class="o">.</span><span class="n">find_one</span><span class="p">())</span>
<span class="go">SON([(u&#39;_id&#39;, 1.0), (u&#39;subdocument&#39;, SON([(u&#39;b&#39;, 1.0), (u&#39;a&#39;, 1.0)]))])</span>
</pre></div>
</div>
<p>The subdocument’s actual storage layout is now visible: “b” is before “a”.</p>
<p>Because a dict’s key order is not defined, you cannot predict how it will be
serialized <strong>to</strong> BSON. But MongoDB considers subdocuments equal only if their
keys have the same order. So if you use a dict to query on a subdocument it may
not match:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collection</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">&#39;subdocument&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">}})</span> <span class="ow">is</span> <span class="kc">None</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Swapping the key order in your query makes no difference:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collection</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">&#39;subdocument&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">}})</span> <span class="ow">is</span> <span class="kc">None</span>
<span class="go">True</span>
</pre></div>
</div>
<p>… because, as we saw above, Python considers the two dicts the same.</p>
<p>There are two solutions. First, you can match the subdocument field-by-field:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collection</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">&#39;subdocument.a&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="s1">&#39;subdocument.b&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">})</span>
<span class="go">{u&#39;_id&#39;: 1.0, u&#39;subdocument&#39;: {u&#39;a&#39;: 1.0, u&#39;b&#39;: 1.0}}</span>
</pre></div>
</div>
<p>The query matches any subdocument with an “a” of 1.0 and a “b” of 1.0,
regardless of the order you specify them in Python or the order they are stored
in BSON. Additionally, this query now matches subdocuments with additional
keys besides “a” and “b”, whereas the previous query required an exact match.</p>
<p>The second solution is to use a <a class="reference internal" href="api/bson/son.html#bson.son.SON" title="bson.son.SON"><code class="xref py py-class docutils literal notranslate"><span class="pre">SON</span></code></a> to specify the key order:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;subdocument&#39;</span><span class="p">:</span> <span class="n">SON</span><span class="p">([(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)])}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collection</span><span class="o">.</span><span class="n">find_one</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<span class="go">{u&#39;_id&#39;: 1.0, u&#39;subdocument&#39;: {u&#39;a&#39;: 1.0, u&#39;b&#39;: 1.0}}</span>
</pre></div>
</div>
<p>The key order you use when you create a <a class="reference internal" href="api/bson/son.html#bson.son.SON" title="bson.son.SON"><code class="xref py py-class docutils literal notranslate"><span class="pre">SON</span></code></a> is preserved
when it is serialized to BSON and used as a query. Thus you can create a
subdocument that exactly matches the subdocument in the collection.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference external" href="http://docs.mongodb.org/manual/tutorial/query-documents/#embedded-documents">MongoDB Manual entry on subdocument matching</a>.</p>
</div>
</div>
<div class="section" id="what-does-cursornotfound-cursor-id-not-valid-at-server-mean">
<h2><a class="toc-backref" href="#id9">What does <em>CursorNotFound</em> cursor id not valid at server mean?</a><a class="headerlink" href="#what-does-cursornotfound-cursor-id-not-valid-at-server-mean" title="Permalink to this headline">¶</a></h2>
<p>Cursors in MongoDB can timeout on the server if they’ve been open for
a long time without any operations being performed on them. This can
lead to an <a class="reference internal" href="api/pymongo/errors.html#pymongo.errors.CursorNotFound" title="pymongo.errors.CursorNotFound"><code class="xref py py-class docutils literal notranslate"><span class="pre">CursorNotFound</span></code></a> exception being
raised when attempting to iterate the cursor.</p>
</div>
<div class="section" id="how-do-i-change-the-timeout-value-for-cursors">
<h2><a class="toc-backref" href="#id10">How do I change the timeout value for cursors?</a><a class="headerlink" href="#how-do-i-change-the-timeout-value-for-cursors" title="Permalink to this headline">¶</a></h2>
<p>MongoDB doesn’t support custom timeouts for cursors, but cursor
timeouts can be turned off entirely. Pass <code class="docutils literal notranslate"><span class="pre">no_cursor_timeout=True</span></code> to
<a class="reference internal" href="api/pymongo/collection.html#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find()</span></code></a>.</p>
</div>
<div class="section" id="how-can-i-store-decimal-decimal-instances">
<h2><a class="toc-backref" href="#id11">How can I store <code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal.Decimal</span></code> instances?</a><a class="headerlink" href="#how-can-i-store-decimal-decimal-instances" title="Permalink to this headline">¶</a></h2>
<p>PyMongo &gt;= 3.4 supports the Decimal128 BSON type introduced in MongoDB 3.4.
See <a class="reference internal" href="api/bson/decimal128.html#module-bson.decimal128" title="bson.decimal128"><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal128</span></code></a> for more information.</p>
<p>MongoDB &lt;= 3.2 only supports IEEE 754 floating points - the same as the
Python float type. The only way PyMongo could store Decimal instances to
these versions of MongoDB would be to convert them to this standard, so
you’d really only be storing floats anyway - we force users to do this
conversion explicitly so that they are aware that it is happening.</p>
</div>
<div class="section" id="i-m-saving-9-99-but-when-i-query-my-document-contains-9-9900000000000002-what-s-going-on-here">
<h2><a class="toc-backref" href="#id12">I’m saving <code class="docutils literal notranslate"><span class="pre">9.99</span></code> but when I query my document contains <code class="docutils literal notranslate"><span class="pre">9.9900000000000002</span></code> - what’s going on here?</a><a class="headerlink" href="#i-m-saving-9-99-but-when-i-query-my-document-contains-9-9900000000000002-what-s-going-on-here" title="Permalink to this headline">¶</a></h2>
<p>The database representation is <code class="docutils literal notranslate"><span class="pre">9.99</span></code> as an IEEE floating point (which
is common to MongoDB and Python as well as most other modern
languages). The problem is that <code class="docutils literal notranslate"><span class="pre">9.99</span></code> cannot be represented exactly
with a double precision floating point - this is true in some versions of
Python as well:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mf">9.99</span>
<span class="go">9.9900000000000002</span>
</pre></div>
</div>
<p>The result that you get when you save <code class="docutils literal notranslate"><span class="pre">9.99</span></code> with PyMongo is exactly the
same as the result you’d get saving it with the JavaScript shell or
any of the other languages (and as the data you’re working with when
you type <code class="docutils literal notranslate"><span class="pre">9.99</span></code> into a Python program).</p>
</div>
<div class="section" id="can-you-add-attribute-style-access-for-documents">
<h2><a class="toc-backref" href="#id13">Can you add attribute style access for documents?</a><a class="headerlink" href="#can-you-add-attribute-style-access-for-documents" title="Permalink to this headline">¶</a></h2>
<p>This request has come up a number of times but we’ve decided not to
implement anything like this. The relevant <a class="reference external" href="http://jira.mongodb.org/browse/PYTHON-35">jira case</a> has some information
about the decision, but here is a brief summary:</p>
<ol class="arabic simple">
<li>This will pollute the attribute namespace for documents, so could
lead to subtle bugs / confusing errors when using a key with the
same name as a dictionary method.</li>
<li>The only reason we even use SON objects instead of regular
dictionaries is to maintain key ordering, since the server
requires this for certain operations. So we’re hesitant to
needlessly complicate SON (at some point it’s hypothetically
possible we might want to revert back to using dictionaries alone,
without breaking backwards compatibility for everyone).</li>
<li>It’s easy (and Pythonic) for new users to deal with documents,
since they behave just like dictionaries. If we start changing
their behavior it adds a barrier to entry for new users - another
class to learn.</li>
</ol>
</div>
<div class="section" id="what-is-the-correct-way-to-handle-time-zones-with-pymongo">
<h2><a class="toc-backref" href="#id14">What is the correct way to handle time zones with PyMongo?</a><a class="headerlink" href="#what-is-the-correct-way-to-handle-time-zones-with-pymongo" title="Permalink to this headline">¶</a></h2>
<p>See <a class="reference internal" href="examples/datetimes.html"><span class="doc">Datetimes and Timezones</span></a> for examples on how to handle
<code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code> objects correctly.</p>
</div>
<div class="section" id="how-can-i-save-a-datetime-date-instance">
<h2><a class="toc-backref" href="#id15">How can I save a <code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime.date</span></code> instance?</a><a class="headerlink" href="#how-can-i-save-a-datetime-date-instance" title="Permalink to this headline">¶</a></h2>
<p>PyMongo doesn’t support saving <code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime.date</span></code> instances, since
there is no BSON type for dates without times. Rather than having the
driver enforce a convention for converting <code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime.date</span></code>
instances to <code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime.datetime</span></code> instances for you, any
conversion should be performed in your client code.</p>
</div>
<div class="section" id="when-i-query-for-a-document-by-objectid-in-my-web-application-i-get-no-result">
<span id="web-application-querying-by-objectid"></span><h2><a class="toc-backref" href="#id16">When I query for a document by ObjectId in my web application I get no result</a><a class="headerlink" href="#when-i-query-for-a-document-by-objectid-in-my-web-application-i-get-no-result" title="Permalink to this headline">¶</a></h2>
<p>It’s common in web applications to encode documents’ ObjectIds in URLs, like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="s2">&quot;/posts/50b3bda58a02fb9a84d8991e&quot;</span>
</pre></div>
</div>
<p>Your web framework will pass the ObjectId portion of the URL to your request
handler as a string, so it must be converted to <a class="reference internal" href="api/bson/objectid.html#bson.objectid.ObjectId" title="bson.objectid.ObjectId"><code class="xref py py-class docutils literal notranslate"><span class="pre">ObjectId</span></code></a>
before it is passed to <a class="reference internal" href="api/pymongo/collection.html#pymongo.collection.Collection.find_one" title="pymongo.collection.Collection.find_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one()</span></code></a>. It is a
common mistake to forget to do this conversion. Here’s how to do it correctly
in <a class="reference external" href="http://flask.pocoo.org/">Flask</a> (other web frameworks are similar):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="kn">from</span> <span class="nn">bson.objectid</span> <span class="k">import</span> <span class="n">ObjectId</span>

<span class="kn">from</span> <span class="nn">flask</span> <span class="k">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">render_template</span>

<span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">()</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>

<span class="nd">@app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s2">&quot;/posts/&lt;_id&gt;&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">show_post</span><span class="p">(</span><span class="n">_id</span><span class="p">):</span>
   <span class="c1"># NOTE!: converting _id from string to ObjectId before passing to find_one</span>
   <span class="n">post</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">posts</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">&#39;_id&#39;</span><span class="p">:</span> <span class="n">ObjectId</span><span class="p">(</span><span class="n">_id</span><span class="p">)})</span>
   <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s1">&#39;post.html&#39;</span><span class="p">,</span> <span class="n">post</span><span class="o">=</span><span class="n">post</span><span class="p">)</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="tutorial.html#querying-by-objectid"><span class="std std-ref">Querying By ObjectId</span></a></p>
</div>
</div>
<div class="section" id="how-can-i-use-pymongo-from-django">
<h2><a class="toc-backref" href="#id17">How can I use PyMongo from Django?</a><a class="headerlink" href="#how-can-i-use-pymongo-from-django" title="Permalink to this headline">¶</a></h2>
<p><a class="reference external" href="http://www.djangoproject.com/">Django</a> is a popular Python web
framework. Django includes an ORM, <code class="xref py py-mod docutils literal notranslate"><span class="pre">django.db</span></code>. Currently,
there’s no official MongoDB backend for Django.</p>
<p><a class="reference external" href="https://django-mongodb-engine.readthedocs.io/">django-mongodb-engine</a>
is an unofficial MongoDB backend that supports Django aggregations, (atomic)
updates, embedded objects, Map/Reduce and GridFS. It allows you to use most
of Django’s built-in features, including the ORM, admin, authentication, site
and session frameworks and caching.</p>
<p>However, it’s easy to use MongoDB (and PyMongo) from Django
without using a Django backend. Certain features of Django that require
<code class="xref py py-mod docutils literal notranslate"><span class="pre">django.db</span></code> (admin, authentication and sessions) will not work
using just MongoDB, but most of what Django provides can still be
used.</p>
<p>One project which should make working with MongoDB and Django easier
is <a class="reference external" href="http://github.com/vpulim/mango">mango</a>. Mango is a set of
MongoDB backends for Django sessions and authentication (bypassing
<code class="xref py py-mod docutils literal notranslate"><span class="pre">django.db</span></code> entirely).</p>
</div>
<div class="section" id="does-pymongo-work-with-mod-wsgi">
<span id="using-with-mod-wsgi"></span><h2><a class="toc-backref" href="#id18">Does PyMongo work with <strong>mod_wsgi</strong>?</a><a class="headerlink" href="#does-pymongo-work-with-mod-wsgi" title="Permalink to this headline">¶</a></h2>
<p>Yes. See the configuration guide for <a class="reference internal" href="examples/mod_wsgi.html#pymongo-and-mod-wsgi"><span class="std std-ref">PyMongo and mod_wsgi</span></a>.</p>
</div>
<div class="section" id="does-pymongo-work-with-pythonanywhere">
<h2><a class="toc-backref" href="#id19">Does PyMongo work with PythonAnywhere?</a><a class="headerlink" href="#does-pymongo-work-with-pythonanywhere" title="Permalink to this headline">¶</a></h2>
<p>No. PyMongo creates Python threads which
<a class="reference external" href="https://www.pythonanywhere.com">PythonAnywhere</a> does not support. For more
information see <a class="reference external" href="https://jira.mongodb.org/browse/PYTHON-1495">PYTHON-1495</a>.</p>
</div>
<div class="section" id="how-can-i-use-something-like-python-s-json-module-to-encode-my-documents-to-json">
<h2><a class="toc-backref" href="#id20">How can I use something like Python’s <code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> module to encode my documents to JSON?</a><a class="headerlink" href="#how-can-i-use-something-like-python-s-json-module-to-encode-my-documents-to-json" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="api/bson/json_util.html#module-bson.json_util" title="bson.json_util: Tools for using Python's json module with BSON documents"><code class="xref py py-mod docutils literal notranslate"><span class="pre">json_util</span></code></a> is PyMongo’s built in, flexible tool for using
Python’s <code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> module with BSON documents and <a class="reference external" href="https://docs.mongodb.com/manual/reference/mongodb-extended-json/">MongoDB Extended JSON</a>. The
<code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> module won’t work out of the box with all documents from PyMongo
as PyMongo supports some special types (like <a class="reference internal" href="api/bson/objectid.html#bson.objectid.ObjectId" title="bson.objectid.ObjectId"><code class="xref py py-class docutils literal notranslate"><span class="pre">ObjectId</span></code></a>
and <a class="reference internal" href="api/bson/dbref.html#bson.dbref.DBRef" title="bson.dbref.DBRef"><code class="xref py py-class docutils literal notranslate"><span class="pre">DBRef</span></code></a>) that are not supported in JSON.</p>
<p><a class="reference external" href="https://pypi.python.org/pypi/python-bsonjs">python-bsonjs</a> is a fast
BSON to MongoDB Extended JSON converter built on top of
<a class="reference external" href="https://github.com/mongodb/libbson">libbson</a>. <cite>python-bsonjs</cite> does not
depend on PyMongo and can offer a nice performance improvement over
<a class="reference internal" href="api/bson/json_util.html#module-bson.json_util" title="bson.json_util: Tools for using Python's json module with BSON documents"><code class="xref py py-mod docutils literal notranslate"><span class="pre">json_util</span></code></a>. <cite>python-bsonjs</cite> works best with PyMongo when using
<a class="reference internal" href="api/bson/raw_bson.html#bson.raw_bson.RawBSONDocument" title="bson.raw_bson.RawBSONDocument"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawBSONDocument</span></code></a>.</p>
</div>
<div class="section" id="why-do-i-get-overflowerror-decoding-dates-stored-by-another-language-s-driver">
<h2><a class="toc-backref" href="#id21">Why do I get OverflowError decoding dates stored by another language’s driver?</a><a class="headerlink" href="#why-do-i-get-overflowerror-decoding-dates-stored-by-another-language-s-driver" title="Permalink to this headline">¶</a></h2>
<p>PyMongo decodes BSON datetime values to instances of Python’s
<code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code>. Instances of <code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code> are
limited to years between <code class="xref py py-data docutils literal notranslate"><span class="pre">datetime.MINYEAR</span></code> (usually 1) and
<code class="xref py py-data docutils literal notranslate"><span class="pre">datetime.MAXYEAR</span></code> (usually 9999). Some MongoDB drivers (e.g. the PHP
driver) can store BSON datetimes with year values far outside those supported
by <code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code>.</p>
<p>There are a few ways to work around this issue. One option is to filter
out documents with values outside of the range supported by
<code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">coll</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">dates</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span> <span class="o">=</span> <span class="n">coll</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s1">&#39;dt&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;$gte&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="s1">&#39;$lte&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">max</span><span class="p">}})</span>
</pre></div>
</div>
<p>Another option, assuming you don’t need the datetime field, is to filter out
just that field:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span> <span class="o">=</span> <span class="n">coll</span><span class="o">.</span><span class="n">find</span><span class="p">({},</span> <span class="n">projection</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;dt&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">})</span>
</pre></div>
</div>
</div>
<div class="section" id="using-pymongo-with-multiprocessing">
<span id="multiprocessing"></span><h2><a class="toc-backref" href="#id22">Using PyMongo with Multiprocessing</a><a class="headerlink" href="#using-pymongo-with-multiprocessing" title="Permalink to this headline">¶</a></h2>
<p>On Unix systems the multiprocessing module spawns processes using <code class="docutils literal notranslate"><span class="pre">fork()</span></code>.
Care must be taken when using instances of
<a class="reference internal" href="api/pymongo/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> with <code class="docutils literal notranslate"><span class="pre">fork()</span></code>. Specifically,
instances of MongoClient must not be copied from a parent process to a child
process. Instead, the parent process and each child process must create their
own instances of MongoClient. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Each process creates its own instance of MongoClient.</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">():</span>
    <span class="n">db</span> <span class="o">=</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">MongoClient</span><span class="p">()</span><span class="o">.</span><span class="n">mydb</span>
    <span class="c1"># Do something with db.</span>

<span class="n">proc</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">func</span><span class="p">)</span>
<span class="n">proc</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>
</div>
<p><strong>Never do this</strong>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">client</span> <span class="o">=</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">MongoClient</span><span class="p">()</span>

<span class="c1"># Each child process attempts to copy a global MongoClient</span>
<span class="c1"># created in the parent process. Never do this.</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">():</span>
  <span class="n">db</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">mydb</span>
  <span class="c1"># Do something with db.</span>

<span class="n">proc</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">func</span><span class="p">)</span>
<span class="n">proc</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>
</div>
<p>Instances of MongoClient copied from the parent process have a high probability
of deadlock in the child process due to
<a class="reference internal" href="#pymongo-fork-safe-details"><span class="std std-ref">inherent incompatibilities between fork(), threads, and locks</span></a>. PyMongo will attempt to issue a warning if there
is a chance of this deadlock occurring.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="#pymongo-fork-safe"><span class="std std-ref">Is PyMongo fork-safe?</span></a></p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Frequently Asked Questions</a><ul>
<li><a class="reference internal" href="#is-pymongo-thread-safe">Is PyMongo thread-safe?</a></li>
<li><a class="reference internal" href="#is-pymongo-fork-safe">Is PyMongo fork-safe?</a></li>
<li><a class="reference internal" href="#how-does-connection-pooling-work-in-pymongo">How does connection pooling work in PyMongo?</a></li>
<li><a class="reference internal" href="#does-pymongo-support-python-3">Does PyMongo support Python 3?</a></li>
<li><a class="reference internal" href="#does-pymongo-support-asynchronous-frameworks-like-gevent-asyncio-tornado-or-twisted">Does PyMongo support asynchronous frameworks like Gevent, asyncio, Tornado, or Twisted?</a></li>
<li><a class="reference internal" href="#why-does-pymongo-add-an-id-field-to-all-of-my-documents">Why does PyMongo add an _id field to all of my documents?</a></li>
<li><a class="reference internal" href="#key-order-in-subdocuments-why-does-my-query-work-in-the-shell-but-not-pymongo">Key order in subdocuments – why does my query work in the shell but not PyMongo?</a></li>
<li><a class="reference internal" href="#what-does-cursornotfound-cursor-id-not-valid-at-server-mean">What does <em>CursorNotFound</em> cursor id not valid at server mean?</a></li>
<li><a class="reference internal" href="#how-do-i-change-the-timeout-value-for-cursors">How do I change the timeout value for cursors?</a></li>
<li><a class="reference internal" href="#how-can-i-store-decimal-decimal-instances">How can I store <code class="docutils literal notranslate"><span class="pre">decimal.Decimal</span></code> instances?</a></li>
<li><a class="reference internal" href="#i-m-saving-9-99-but-when-i-query-my-document-contains-9-9900000000000002-what-s-going-on-here">I’m saving <code class="docutils literal notranslate"><span class="pre">9.99</span></code> but when I query my document contains <code class="docutils literal notranslate"><span class="pre">9.9900000000000002</span></code> - what’s going on here?</a></li>
<li><a class="reference internal" href="#can-you-add-attribute-style-access-for-documents">Can you add attribute style access for documents?</a></li>
<li><a class="reference internal" href="#what-is-the-correct-way-to-handle-time-zones-with-pymongo">What is the correct way to handle time zones with PyMongo?</a></li>
<li><a class="reference internal" href="#how-can-i-save-a-datetime-date-instance">How can I save a <code class="docutils literal notranslate"><span class="pre">datetime.date</span></code> instance?</a></li>
<li><a class="reference internal" href="#when-i-query-for-a-document-by-objectid-in-my-web-application-i-get-no-result">When I query for a document by ObjectId in my web application I get no result</a></li>
<li><a class="reference internal" href="#how-can-i-use-pymongo-from-django">How can I use PyMongo from Django?</a></li>
<li><a class="reference internal" href="#does-pymongo-work-with-mod-wsgi">Does PyMongo work with <strong>mod_wsgi</strong>?</a></li>
<li><a class="reference internal" href="#does-pymongo-work-with-pythonanywhere">Does PyMongo work with PythonAnywhere?</a></li>
<li><a class="reference internal" href="#how-can-i-use-something-like-python-s-json-module-to-encode-my-documents-to-json">How can I use something like Python’s <code class="docutils literal notranslate"><span class="pre">json</span></code> module to encode my documents to JSON?</a></li>
<li><a class="reference internal" href="#why-do-i-get-overflowerror-decoding-dates-stored-by-another-language-s-driver">Why do I get OverflowError decoding dates stored by another language’s driver?</a></li>
<li><a class="reference internal" href="#using-pymongo-with-multiprocessing">Using PyMongo with Multiprocessing</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="examples/tls.html"
                        title="previous chapter">TLS/SSL and PyMongo</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="compatibility-policy.html"
                        title="next chapter">Compatibility Policy</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/faq.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="compatibility-policy.html" title="Compatibility Policy"
             >next</a> |</li>
        <li class="right" >
          <a href="examples/tls.html" title="TLS/SSL and PyMongo"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyMongo 3.7.2 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright MongoDB, Inc. 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
    </div>
  </body>
</html>