<!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>objectid – Tools for working with MongoDB ObjectIds — 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="raw_bson – Tools for representing raw BSON documents." href="raw_bson.html" /> <link rel="prev" title="min_key – Representation for the MongoDB internal MinKey type" href="min_key.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="raw_bson.html" title="raw_bson – Tools for representing raw BSON documents." accesskey="N">next</a> |</li> <li class="right" > <a href="min_key.html" title="min_key – Representation for the MongoDB internal MinKey type" accesskey="P">previous</a> |</li> <li class="nav-item nav-item-0"><a href="../../index.html">PyMongo 3.7.2 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="../index.html" >API Documentation</a> »</li> <li class="nav-item nav-item-2"><a href="index.html" accesskey="U"><code class="docutils literal notranslate"><span class="pre">bson</span></code> – BSON (Binary JSON) Encoding and Decoding</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-bson.objectid"> <span id="objectid-tools-for-working-with-mongodb-objectids"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">objectid</span></code> – Tools for working with MongoDB ObjectIds<a class="headerlink" href="#module-bson.objectid" title="Permalink to this headline">¶</a></h1> <p>Tools for working with MongoDB <a class="reference external" href="http://dochub.mongodb.org/core/objectids">ObjectIds</a>.</p> <dl class="class"> <dt id="bson.objectid.ObjectId"> <em class="property">class </em><code class="descclassname">bson.objectid.</code><code class="descname">ObjectId</code><span class="sig-paren">(</span><em>oid=None</em><span class="sig-paren">)</span><a class="headerlink" href="#bson.objectid.ObjectId" title="Permalink to this definition">¶</a></dt> <dd><p>Initialize a new ObjectId.</p> <p>An ObjectId is a 12-byte unique identifier consisting of:</p> <blockquote> <div><ul class="simple"> <li>a 4-byte value representing the seconds since the Unix epoch,</li> <li>a 3-byte machine identifier,</li> <li>a 2-byte process id, and</li> <li>a 3-byte counter, starting with a random value.</li> </ul> </div></blockquote> <p>By default, <code class="docutils literal notranslate"><span class="pre">ObjectId()</span></code> creates a new unique identifier. The optional parameter <cite>oid</cite> can be an <a class="reference internal" href="#bson.objectid.ObjectId" title="bson.objectid.ObjectId"><code class="xref py py-class docutils literal notranslate"><span class="pre">ObjectId</span></code></a>, or any 12 <code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code> or, in Python 2, any 12-character <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code>.</p> <p>For example, the 12 bytes b’foo-bar-quux’ do not follow the ObjectId specification but they are acceptable input:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">ObjectId</span><span class="p">(</span><span class="sa">b</span><span class="s1">'foo-bar-quux'</span><span class="p">)</span> <span class="go">ObjectId('666f6f2d6261722d71757578')</span> </pre></div> </div> <p><cite>oid</cite> can also be a <code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code> or <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code> of 24 hex digits:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">ObjectId</span><span class="p">(</span><span class="s1">'0123456789ab0123456789ab'</span><span class="p">)</span> <span class="go">ObjectId('0123456789ab0123456789ab')</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c1"># A u-prefixed unicode literal:</span> <span class="gp">>>> </span><span class="n">ObjectId</span><span class="p">(</span><span class="sa">u</span><span class="s1">'0123456789ab0123456789ab'</span><span class="p">)</span> <span class="go">ObjectId('0123456789ab0123456789ab')</span> </pre></div> </div> <p>Raises <a class="reference internal" href="errors.html#bson.errors.InvalidId" title="bson.errors.InvalidId"><code class="xref py py-class docutils literal notranslate"><span class="pre">InvalidId</span></code></a> if <cite>oid</cite> is not 12 bytes nor 24 hex digits, or <code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code> if <cite>oid</cite> is not an accepted type.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <li><cite>oid</cite> (optional): a valid ObjectId.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="admonition-title">The MongoDB documentation on</p> <p class="last"><a class="reference external" href="http://dochub.mongodb.org/core/objectids" name="bson.objectid.ObjectId"><em>objectids</em></a></p> </div> <dl class="describe"> <dt> <code class="descname">str(o)</code></dt> <dd><p>Get a hex encoded version of <a class="reference internal" href="#bson.objectid.ObjectId" title="bson.objectid.ObjectId"><code class="xref py py-class docutils literal notranslate"><span class="pre">ObjectId</span></code></a> <cite>o</cite>.</p> <p>The following property always holds:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">o</span> <span class="o">=</span> <span class="n">ObjectId</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">o</span> <span class="o">==</span> <span class="n">ObjectId</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">o</span><span class="p">))</span> <span class="go">True</span> </pre></div> </div> <p>This representation is useful for urls or other places where <code class="docutils literal notranslate"><span class="pre">o.binary</span></code> is inappropriate.</p> </dd></dl> <dl class="attribute"> <dt id="bson.objectid.ObjectId.binary"> <code class="descname">binary</code><a class="headerlink" href="#bson.objectid.ObjectId.binary" title="Permalink to this definition">¶</a></dt> <dd><p>12-byte binary representation of this ObjectId.</p> </dd></dl> <dl class="classmethod"> <dt id="bson.objectid.ObjectId.from_datetime"> <em class="property">classmethod </em><code class="descname">from_datetime</code><span class="sig-paren">(</span><em>generation_time</em><span class="sig-paren">)</span><a class="headerlink" href="#bson.objectid.ObjectId.from_datetime" title="Permalink to this definition">¶</a></dt> <dd><p>Create a dummy ObjectId instance with a specific generation time.</p> <p>This method is useful for doing range queries on a field containing <a class="reference internal" href="#bson.objectid.ObjectId" title="bson.objectid.ObjectId"><code class="xref py py-class docutils literal notranslate"><span class="pre">ObjectId</span></code></a> instances.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">It is not safe to insert a document containing an ObjectId generated using this method. This method deliberately eliminates the uniqueness guarantee that ObjectIds generally provide. ObjectIds generated with this method should be used exclusively in queries.</p> </div> <p><cite>generation_time</cite> will be converted to UTC. Naive datetime instances will be treated as though they already contain UTC.</p> <p>An example using this helper to get documents where <code class="docutils literal notranslate"><span class="pre">"_id"</span></code> was generated before January 1, 2010 would be:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">gen_time</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2010</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">dummy_id</span> <span class="o">=</span> <span class="n">ObjectId</span><span class="o">.</span><span class="n">from_datetime</span><span class="p">(</span><span class="n">gen_time</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s2">"_id"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"$lt"</span><span class="p">:</span> <span class="n">dummy_id</span><span class="p">}})</span> </pre></div> </div> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <li><cite>generation_time</cite>: <code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code> to be used as the generation time for the resulting ObjectId.</li> </ul> </td> </tr> </tbody> </table> </dd></dl> <dl class="attribute"> <dt id="bson.objectid.ObjectId.generation_time"> <code class="descname">generation_time</code><a class="headerlink" href="#bson.objectid.ObjectId.generation_time" title="Permalink to this definition">¶</a></dt> <dd><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code> instance representing the time of generation for this <a class="reference internal" href="#bson.objectid.ObjectId" title="bson.objectid.ObjectId"><code class="xref py py-class docutils literal notranslate"><span class="pre">ObjectId</span></code></a>.</p> <p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code> is timezone aware, and represents the generation time in UTC. It is precise to the second.</p> </dd></dl> <dl class="classmethod"> <dt id="bson.objectid.ObjectId.is_valid"> <em class="property">classmethod </em><code class="descname">is_valid</code><span class="sig-paren">(</span><em>oid</em><span class="sig-paren">)</span><a class="headerlink" href="#bson.objectid.ObjectId.is_valid" title="Permalink to this definition">¶</a></dt> <dd><p>Checks if a <cite>oid</cite> string is valid or not.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <li><cite>oid</cite>: the object id to validate</li> </ul> </td> </tr> </tbody> </table> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> </dd></dl> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="min_key.html" title="previous chapter"><code class="docutils literal notranslate"><span class="pre">min_key</span></code> – Representation for the MongoDB internal MinKey type</a></p> <h4>Next topic</h4> <p class="topless"><a href="raw_bson.html" title="next chapter"><code class="docutils literal notranslate"><span class="pre">raw_bson</span></code> – Tools for representing raw BSON documents.</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/api/bson/objectid.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="raw_bson.html" title="raw_bson – Tools for representing raw BSON documents." >next</a> |</li> <li class="right" > <a href="min_key.html" title="min_key – Representation for the MongoDB internal MinKey type" >previous</a> |</li> <li class="nav-item nav-item-0"><a href="../../index.html">PyMongo 3.7.2 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="../index.html" >API Documentation</a> »</li> <li class="nav-item nav-item-2"><a href="index.html" ><code class="docutils literal notranslate"><span class="pre">bson</span></code> – BSON (Binary JSON) Encoding and Decoding</a> »</li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright MongoDB, Inc. 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc. </div> </body> </html>