This file is indexed.

/usr/share/doc/python-pymongo-doc/html/faq.html is in python-pymongo-doc 2.6.3-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<!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="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Frequently Asked Questions &mdash; PyMongo 2.6.3 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '2.6.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </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/sidebar.js"></script>
    <link rel="top" title="PyMongo 2.6.3 documentation" href="index.html" />
    <link rel="next" title="API Documentation" href="api/index.html" />
    <link rel="prev" title="Requests" href="examples/requests.html" /> 
  </head>
  <body>
    <div class="related">
      <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="api/index.html" title="API Documentation"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="examples/requests.html" title="Requests"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">PyMongo 2.6.3 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="frequently-asked-questions">
<h1><a class="toc-backref" href="#id2">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="id2">Frequently Asked Questions</a><ul>
<li><a class="reference internal" href="#is-pymongo-thread-safe" id="id3">Is PyMongo thread-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-tornado-or-twisted" id="id6">Does PyMongo support asynchronous frameworks like Gevent, Tornado, or Twisted?</a></li>
<li><a class="reference internal" href="#what-does-operationfailure-cursor-id-not-valid-at-server-mean" id="id7">What does <em>OperationFailure</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="id8">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="id9">How can I store <tt class="xref py py-mod docutils literal"><span class="pre">decimal.Decimal</span></tt> 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="id10">I&#8217;m saving <tt class="docutils literal"><span class="pre">9.99</span></tt> but when I query my document contains <tt class="docutils literal"><span class="pre">9.9900000000000002</span></tt> - what&#8217;s going on here?</a></li>
<li><a class="reference internal" href="#can-you-add-attribute-style-access-for-documents" id="id11">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="id12">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="id13">How can I save a <tt class="xref py py-mod docutils literal"><span class="pre">datetime.date</span></tt> 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="id14">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="id15">How can I use PyMongo from Django?</a></li>
<li><a class="reference internal" href="#does-pymongo-work-with-mod-wsgi" id="id16">Does PyMongo work with <strong>mod_wsgi</strong>?</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="id17">How can I use something like Python&#8217;s <tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt> module to encode my documents to JSON?</a></li>
<li><a class="reference internal" href="#why-do-i-get-an-error-for-dates-on-or-after-2038" id="id18">Why do I get an error for dates on or after 2038?</a></li>
<li><a class="reference internal" href="#why-do-i-get-overflowerror-decoding-dates-stored-by-another-language-s-driver" id="id19">Why do I get OverflowError decoding dates stored by another language&#8217;s driver?</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="is-pymongo-thread-safe">
<h2><a class="toc-backref" href="#id3">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 even provides built-in connection pooling
for threaded applications.</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"><tt class="xref py py-class docutils literal"><span class="pre">MongoClient</span></tt></a> instance has a built-in
connection pool. The pool begins with one open connection. If necessary to
support concurrent access to MongoDB from multiple threads in your application,
the client opens new connections on demand.</p>
<p>By default, there is no thread-affinity for connections.</p>
<p>In versions before 2.6, the default <tt class="docutils literal"><span class="pre">max_pool_size</span></tt> was 10, and it did not
actually bound the number of open connections; it only determined the number
of connections that would be kept open when no longer in use.</p>
<p>Starting with PyMongo 2.6, the size of the connection pool is capped at
<tt class="docutils literal"><span class="pre">max_pool_size</span></tt>, which now defaults to 100. When a thread in your application
begins an operation on MongoDB, if all other connections are in use and the
pool has reached its maximum, the thread pauses, waiting for a connection to
be returned to the pool by another thread.</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"><tt class="xref py py-class docutils literal"><span class="pre">MongoClient</span></tt></a>
works for most applications:</p>
<div class="highlight-python"><div class="highlight"><pre><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> when your program starts up, 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 <tt class="docutils literal"><span class="pre">max_pool_size</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><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">max_pool_size</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-python"><div class="highlight"><pre><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">max_pool_size</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p>By default, any number of threads are allowed to wait for connections to become
available, and they can wait any length of time. Override <tt class="docutils literal"><span class="pre">waitQueueMultiple</span></tt>
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-python"><div class="highlight"><pre><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">max_pool_size</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 connection
raises <a class="reference internal" href="api/pymongo/errors.html#pymongo.errors.ExceededMaxWaiters" title="pymongo.errors.ExceededMaxWaiters"><tt class="xref py py-exc docutils literal"><span class="pre">ExceededMaxWaiters</span></tt></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 connections to become available, unless you set
<tt class="docutils literal"><span class="pre">waitQueueTimeoutMS</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><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 connection raises
<a class="reference internal" href="api/pymongo/errors.html#pymongo.errors.ConnectionFailure" title="pymongo.errors.ConnectionFailure"><tt class="xref py py-exc docutils literal"><span class="pre">ConnectionFailure</span></tt></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.disconnect" title="pymongo.mongo_client.MongoClient.disconnect"><tt class="xref py py-meth docutils literal"><span class="pre">disconnect()</span></tt></a> is called by any thread,
all sockets are closed.</p>
<p><a class="reference internal" href="api/pymongo/mongo_replica_set_client.html#pymongo.mongo_replica_set_client.MongoReplicaSetClient" title="pymongo.mongo_replica_set_client.MongoReplicaSetClient"><tt class="xref py py-class docutils literal"><span class="pre">MongoReplicaSetClient</span></tt></a> maintains one
connection pool per server in your replica set.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="examples/requests.html"><em>Requests</em></a></p>
</div>
</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>Starting with version 2.2 PyMongo supports Python 3.x where x &gt;= 1. See the
<a class="reference internal" href="python3.html"><em>Python 3 FAQ</em></a> for details.</p>
</div>
<div class="section" id="does-pymongo-support-asynchronous-frameworks-like-gevent-tornado-or-twisted">
<h2><a class="toc-backref" href="#id6">Does PyMongo support asynchronous frameworks like Gevent, Tornado, or Twisted?</a><a class="headerlink" href="#does-pymongo-support-asynchronous-frameworks-like-gevent-tornado-or-twisted" title="Permalink to this headline"></a></h2>
<p>PyMongo fully supports <a class="reference internal" href="examples/gevent.html"><em>Gevent</em></a>.</p>
<p>To use MongoDB with <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="http://github.com/fiorix/mongo-async-python-driver">TxMongo</a>. Compared to PyMongo,
TxMongo is less stable, lack features, and is less actively maintained.</p>
</div>
<div class="section" id="what-does-operationfailure-cursor-id-not-valid-at-server-mean">
<h2><a class="toc-backref" href="#id7">What does <em>OperationFailure</em> cursor id not valid at server mean?</a><a class="headerlink" href="#what-does-operationfailure-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&#8217;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.OperationFailure" title="pymongo.errors.OperationFailure"><tt class="xref py py-class docutils literal"><span class="pre">OperationFailure</span></tt></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="#id8">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&#8217;t support custom timeouts for cursors, but cursor
timeouts can be turned off entirely. Pass <tt class="docutils literal"><span class="pre">timeout=False</span></tt> to
<a class="reference internal" href="api/pymongo/collection.html#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt></a>.</p>
</div>
<div class="section" id="how-can-i-store-decimal-decimal-instances">
<h2><a class="toc-backref" href="#id9">How can I store <tt class="xref py py-mod docutils literal"><span class="pre">decimal.Decimal</span></tt> instances?</a><a class="headerlink" href="#how-can-i-store-decimal-decimal-instances" title="Permalink to this headline"></a></h2>
<p>MongoDB only supports IEEE 754 floating points - the same as the
Python float type. The only way PyMongo could store Decimal instances
would be to convert them to this standard, so you&#8217;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="#id10">I&#8217;m saving <tt class="docutils literal"><span class="pre">9.99</span></tt> but when I query my document contains <tt class="docutils literal"><span class="pre">9.9900000000000002</span></tt> - what&#8217;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 <tt class="docutils literal"><span class="pre">9.99</span></tt> as an IEEE floating point (which
is common to MongoDB and Python as well as most other modern
languages). The problem is that <tt class="docutils literal"><span class="pre">9.99</span></tt> cannot be represented exactly
with a double precision floating point - this is true in some versions of
Python as well:</p>
<div class="highlight-python"><div class="highlight"><pre><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 <tt class="docutils literal"><span class="pre">9.99</span></tt> with PyMongo is exactly the
same as the result you&#8217;d get saving it with the JavaScript shell or
any of the other languages (and as the data you&#8217;re working with when
you type <tt class="docutils literal"><span class="pre">9.99</span></tt> into a Python program).</p>
</div>
<div class="section" id="can-you-add-attribute-style-access-for-documents">
<h2><a class="toc-backref" href="#id11">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&#8217;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&#8217;re hesitant to
needlessly complicate SON (at some point it&#8217;s hypothetically
possible we might want to revert back to using dictionaries alone,
without breaking backwards compatibility for everyone).</li>
<li>It&#8217;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="#id12">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>Prior to PyMongo version 1.7, the correct way is to only save naive
<tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt> instances, and to save all dates as
UTC. In versions &gt;= 1.7, the driver will automatically convert aware
datetimes to UTC before saving them. By default, datetimes retrieved
from the server (no matter what version of the driver you&#8217;re using)
will be naive and represent UTC. In newer versions of the driver you
can set the <a class="reference internal" href="api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient" title="pymongo.mongo_client.MongoClient"><tt class="xref py py-class docutils literal"><span class="pre">MongoClient</span></tt></a> <cite>tz_aware</cite>
parameter to <tt class="docutils literal"><span class="pre">True</span></tt>, which will cause all
<tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt> instances returned from that MongoClient to
be aware (UTC). This setting is recommended, as it can force
application code to handle timezones properly.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Be careful not to save naive <tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt>
instances that are not UTC (i.e. the result of calling
<tt class="xref py py-meth docutils literal"><span class="pre">datetime.datetime.now()</span></tt>).</p>
</div>
<p>Something like <tt class="xref py py-mod docutils literal"><span class="pre">pytz</span></tt> can be used to convert dates to localtime
after retrieving them from the database.</p>
</div>
<div class="section" id="how-can-i-save-a-datetime-date-instance">
<h2><a class="toc-backref" href="#id13">How can I save a <tt class="xref py py-mod docutils literal"><span class="pre">datetime.date</span></tt> instance?</a><a class="headerlink" href="#how-can-i-save-a-datetime-date-instance" title="Permalink to this headline"></a></h2>
<p>PyMongo doesn&#8217;t support saving <tt class="xref py py-mod docutils literal"><span class="pre">datetime.date</span></tt> instances, since
there is no BSON type for dates without times. Rather than having the
driver enforce a convention for converting <tt class="xref py py-mod docutils literal"><span class="pre">datetime.date</span></tt>
instances to <tt class="xref py py-mod docutils literal"><span class="pre">datetime.datetime</span></tt> 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="#id14">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&#8217;s common in web applications to encode documents&#8217; ObjectIds in URLs, like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">&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"><tt class="xref py py-class docutils literal"><span class="pre">ObjectId</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">find_one()</span></tt></a>. It is a
common mistake to forget to do this conversion. Here&#8217;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-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pymongo</span> <span class="kn">import</span> <span class="n">MongoClient</span>
<span class="kn">from</span> <span class="nn">bson.objectid</span> <span class="kn">import</span> <span class="n">ObjectId</span>

<span class="kn">from</span> <span class="nn">flask</span> <span class="kn">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="n">__name__</span><span class="p">)</span>

<span class="nd">@app.route</span><span class="p">(</span><span class="s">&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="c"># 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="s">&#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="s">&#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="n">__name__</span> <span class="o">==</span> <span class="s">&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"><em>Querying By ObjectId</em></a></p>
</div>
</div>
<div class="section" id="how-can-i-use-pymongo-from-django">
<h2><a class="toc-backref" href="#id15">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, <tt class="xref py py-mod docutils literal"><span class="pre">django.db</span></tt>. Currently,
there&#8217;s no official MongoDB backend for Django.</p>
<p><a class="reference external" href="https://django-mongodb-engine.readthedocs.org/">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&#8217;s built-in features, including the ORM, admin, authentication, site
and session frameworks and caching.</p>
<p>However, it&#8217;s easy to use MongoDB (and PyMongo) from Django
without using a Django backend. Certain features of Django that require
<tt class="xref py py-mod docutils literal"><span class="pre">django.db</span></tt> (admin, authentication and sessions) will not work
using just MongoDB, but most of what Django provides can still be
used.</p>
<p>We have written a demo <a class="reference external" href="http://github.com/mdirolf/DjanMon/tree/master">Django + MongoDB project</a>. The README for that
project describes some of what you need to do to use MongoDB from
Django. The main point is that your persistence code will go directly
into your views, rather than being defined in separate models. The
README also gives instructions for how to change settings.py to
disable the features that won&#8217;t work with MongoDB.</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
<tt class="xref py py-mod docutils literal"><span class="pre">django.db</span></tt> 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="#id16">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><a class="reference external" href="http://code.google.com/p/modwsgi/">mod_wsgi</a> is a popular Apache
module used for hosting Python applications conforming to the <a class="reference external" href="http://www.wsgi.org/">wsgi</a> specification. There is a potential issue
when deploying PyMongo applications with mod_wsgi involving PyMongo&#8217;s
C extension and mod_wsgi&#8217;s multiple sub interpreters.</p>
<p>One tricky issue that we&#8217;ve seen when deploying PyMongo applications
with mod_wsgi is documented <a class="reference external" href="http://code.google.com/p/modwsgi/wiki/ApplicationIssues">here</a>, in the
<strong>Multiple Python Sub Interpreters</strong> section. When running PyMongo
with the C extension enabled it is possible to see strange failures
when encoding due to the way mod_wsgi handles module reloading with
multiple sub interpreters. There are several possible ways to work
around this issue:</p>
<ol class="arabic simple">
<li>Run mod_wsgi in daemon mode with each WSGI application assigned to its
own daemon process.</li>
<li>Force all WSGI applications to run in the same application group.</li>
<li>Install PyMongo <a class="reference internal" href="installation.html#install-no-c"><em>without the C extension</em></a> (this will
carry a performance penalty, but is the most immediate solution to this
problem).</li>
</ol>
</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="#id17">How can I use something like Python&#8217;s <tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt> 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>The <tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt> module won&#8217;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"><tt class="xref py py-class docutils literal"><span class="pre">ObjectId</span></tt></a> and <a class="reference internal" href="api/bson/dbref.html#bson.dbref.DBRef" title="bson.dbref.DBRef"><tt class="xref py py-class docutils literal"><span class="pre">DBRef</span></tt></a>)
that are not supported in JSON. We&#8217;ve added some utilities for working
with <tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt> and <tt class="xref py py-mod docutils literal"><span class="pre">simplejson</span></tt> in the
<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"><tt class="xref py py-mod docutils literal"><span class="pre">json_util</span></tt></a> module.</p>
</div>
<div class="section" id="why-do-i-get-an-error-for-dates-on-or-after-2038">
<span id="year-2038-problem"></span><h2><a class="toc-backref" href="#id18">Why do I get an error for dates on or after 2038?</a><a class="headerlink" href="#why-do-i-get-an-error-for-dates-on-or-after-2038" title="Permalink to this headline"></a></h2>
<p>On Unix systems, dates are represented as seconds from 1 January 1970 and
usually stored in the C <tt class="xref py py-mod docutils literal"><span class="pre">time_t</span></tt> type. On most 32-bit operating systems
<tt class="xref py py-mod docutils literal"><span class="pre">time_t</span></tt> is a signed 4 byte integer which means it can&#8217;t handle dates
after 19 January 2038; this is known as the <a class="reference external" href="http://en.wikipedia.org/wiki/Year_2038_problem">year 2038 problem</a>. Neither MongoDB nor Python
uses <tt class="xref py py-mod docutils literal"><span class="pre">time_t</span></tt> to represent dates internally so do not suffer from this
problem, but Python&#8217;s <tt class="xref py py-mod docutils literal"><span class="pre">datetime.datetime.fromtimestamp()</span></tt> does, which
means it is susceptible.</p>
<p>Previous to version 2.0, PyMongo used <tt class="xref py py-mod docutils literal"><span class="pre">datetime.datetime.fromtimestamp()</span></tt>
in its pure Python <a class="reference internal" href="api/bson/index.html#module-bson" title="bson: BSON (Binary JSON) Encoding and Decoding"><tt class="xref py py-mod docutils literal"><span class="pre">bson</span></tt></a> module. Therefore, on 32-bit systems you may
get an error retrieving dates after 2038 from MongoDB using older versions
of PyMongo with the pure Python version of <a class="reference internal" href="api/bson/index.html#module-bson" title="bson: BSON (Binary JSON) Encoding and Decoding"><tt class="xref py py-mod docutils literal"><span class="pre">bson</span></tt></a>.</p>
<p>This problem was fixed in the pure Python implementation of <a class="reference internal" href="api/bson/index.html#module-bson" title="bson: BSON (Binary JSON) Encoding and Decoding"><tt class="xref py py-mod docutils literal"><span class="pre">bson</span></tt></a> by
commit <tt class="docutils literal"><span class="pre">b19ab334af2a29353529</span></tt> (8 June 2011 - PyMongo 2.0).</p>
<p>The C implementation of <a class="reference internal" href="api/bson/index.html#module-bson" title="bson: BSON (Binary JSON) Encoding and Decoding"><tt class="xref py py-mod docutils literal"><span class="pre">bson</span></tt></a> also used to suffer from this problem but
it was fixed in commit <tt class="docutils literal"><span class="pre">566bc9fb7be6f9ab2604</span></tt> (10 May 2010 - PyMongo 1.7).</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="#id19">Why do I get OverflowError decoding dates stored by another language&#8217;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&#8217;s
<tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt>. Instances of <tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt> are
limited to years between <tt class="xref py py-data docutils literal"><span class="pre">datetime.MINYEAR</span></tt> (usually 1) and
<tt class="xref py py-data docutils literal"><span class="pre">datetime.MAXYEAR</span></tt> (usually 9999). Some MongoDB drivers (e.g. the PHP
driver) can store BSON datetimes with year values far outside those supported
by <tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt>.</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
<tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">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="s">&#39;dt&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#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="s">&#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&#8217;t need the datetime field, is to filter out
just that field:</p>
<div class="highlight-python"><div class="highlight"><pre><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">fields</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;dt&#39;</span><span class="p">:</span> <span class="bp">False</span><span class="p">})</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <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="#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-tornado-or-twisted">Does PyMongo support asynchronous frameworks like Gevent, Tornado, or Twisted?</a></li>
<li><a class="reference internal" href="#what-does-operationfailure-cursor-id-not-valid-at-server-mean">What does <em>OperationFailure</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 <tt class="docutils literal"><span class="pre">decimal.Decimal</span></tt> 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&#8217;m saving <tt class="docutils literal"><span class="pre">9.99</span></tt> but when I query my document contains <tt class="docutils literal"><span class="pre">9.9900000000000002</span></tt> - what&#8217;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 <tt class="docutils literal"><span class="pre">datetime.date</span></tt> 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="#how-can-i-use-something-like-python-s-json-module-to-encode-my-documents-to-json">How can I use something like Python&#8217;s <tt class="docutils literal"><span class="pre">json</span></tt> module to encode my documents to JSON?</a></li>
<li><a class="reference internal" href="#why-do-i-get-an-error-for-dates-on-or-after-2038">Why do I get an error for dates on or after 2038?</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&#8217;s driver?</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="examples/requests.html"
                        title="previous chapter">Requests</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="api/index.html"
                        title="next chapter">API Documentation</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/faq.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <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>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <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="api/index.html" title="API Documentation"
             >next</a> |</li>
        <li class="right" >
          <a href="examples/requests.html" title="Requests"
             >previous</a> |</li>
        <li><a href="index.html">PyMongo 2.6.3 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2008 - 2012, 10gen, Inc..
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.
    </div>
  </body>
</html>