<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>The MongoDB\BSON\ObjectId class</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb-bson-minkey.unserialize.html">MongoDB\BSON\MinKey::unserialize</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb-bson-objectid.construct.html">MongoDB\BSON\ObjectId::__construct</a></div> <div class="up"><a href="book.bson.html">MongoDB\BSON</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="class.mongodb-bson-objectid" class="reference"> <h1 class="title">The MongoDB\BSON\ObjectId class</h1> <div class="partintro"><p class="verinfo">(mongodb >=1.0.0)</p> <div class="section" id="mongodb-bson-objectid.intro"> <h2 class="title">Introduction</h2> <p class="para"> BSON type for an <a href="https://docs.mongodb.com/manual/reference/bson-types/#objectid" class="link external">» ObjectId</a>. The value consists of 12 bytes, where the first four bytes are a timestamp that reflect the ObjectId's creation. Specifically, the value consists of: </p> <ul class="itemizedlist"> <li class="listitem"><span class="simpara">a 4-byte value representing the seconds since the Unix epoch,</span></li> <li class="listitem"><span class="simpara">a 3-byte machine identifier,</span></li> <li class="listitem"><span class="simpara">a 2-byte process ID, and</span></li> <li class="listitem"><span class="simpara">a 3-byte counter, starting with a random value.</span></li> </ul> <p class="para"> In MongoDB, each document stored in a collection requires a unique <em>_id</em> field that acts as a primary key. If an inserted document omits the <em>_id</em> field, the driver automatically generates an ObjectId for the <em>_id</em> field. </p> <p class="para"> Using ObjectIds for the <em>_id</em> field provides the following additional benefits: </p> <ul class="itemizedlist"> <li class="listitem"><span class="simpara">The creation time of the ObjectId may be accessed using the <span class="methodname"><a href="mongodb-bson-objectid.gettimestamp.html" class="methodname">MongoDB\BSON\ObjectId::getTimestamp()</a></span> method.</span></li> <li class="listitem"><span class="simpara">Sorting on an <em>_id</em> field that stores ObjectId values is roughly equivalent to sorting by creation time.</span></li> </ul> </div> <div class="section" id="mongodb-bson-objectid.synopsis"> <h2 class="title">Class synopsis</h2> <div class="classsynopsis"> <div class="ooclass"></div> <div class="classsynopsisinfo"> <span class="modifier">final</span> <span class="ooclass"> <strong class="classname">MongoDB\BSON\ObjectId</strong> </span> <span class="oointerface">implements <span class="interfacename"><a href="class.mongodb-bson-objectidinterface.html" class="interfacename">MongoDB\BSON\ObjectIdInterface</a></span> </span> <span class="oointerface">, <span class="interfacename"><a href="class.mongodb-bson-type.html" class="interfacename">MongoDB\BSON\Type</a></span> </span> <span class="oointerface">, <span class="interfacename"><a href="class.serializable.html" class="interfacename">Serializable</a></span> </span> <span class="oointerface">, <span class="interfacename"><a href="class.jsonserializable.html" class="interfacename">JsonSerializable</a></span> </span> {</div> <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div> <div class="methodsynopsis dc-description"> <span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-bson-objectid.construct.html" class="methodname">__construct</a></span> ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$id</code></span> ] )</div> <div class="methodsynopsis dc-description"> <span class="modifier">final</span> <span class="modifier">public</span> <span class="type">int</span> <span class="methodname"><a href="mongodb-bson-objectid.gettimestamp.html" class="methodname">getTimestamp</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">final</span> <span class="modifier">public</span> <span class="type">mixed</span> <span class="methodname"><a href="mongodb-bson-objectid.jsonserialize.html" class="methodname">jsonSerialize</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">final</span> <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><a href="mongodb-bson-objectid.serialize.html" class="methodname">serialize</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">final</span> <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><a href="mongodb-bson-objectid.tostring.html" class="methodname">__toString</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">final</span> <span class="modifier">public</span> <span class="type">void</span> <span class="methodname"><a href="mongodb-bson-objectid.unserialize.html" class="methodname">unserialize</a></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$serialized</code></span> )</div> }</div> </div> <div class="section"> <h2 class="title">Changelog</h2> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>1.3.0</td> <td> <p class="para"> Renamed from <em>MongoDB\BSON\ObjectID</em> to <em>MongoDB\BSON\ObjectId</em>. </p> </td> </tr> <tr> <td>1.2.0</td> <td> Implements <span class="interfacename"><a href="class.serializable.html" class="interfacename">Serializable</a></span> and <span class="interfacename"><a href="class.jsonserializable.html" class="interfacename">JsonSerializable</a></span>. </td> </tr> </tbody> </table> </p> </div> </div> <h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="mongodb-bson-objectid.construct.html">MongoDB\BSON\ObjectId::__construct</a> — Construct a new ObjectId</li><li><a href="mongodb-bson-objectid.gettimestamp.html">MongoDB\BSON\ObjectId::getTimestamp</a> — Returns the timestamp component of this ObjectId</li><li><a href="mongodb-bson-objectid.jsonserialize.html">MongoDB\BSON\ObjectId::jsonSerialize</a> — Returns a representation that can be converted to JSON</li><li><a href="mongodb-bson-objectid.serialize.html">MongoDB\BSON\ObjectId::serialize</a> — Serialize an ObjectId</li><li><a href="mongodb-bson-objectid.tostring.html">MongoDB\BSON\ObjectId::__toString</a> — Returns the hexidecimal representation of this ObjectId</li><li><a href="mongodb-bson-objectid.unserialize.html">MongoDB\BSON\ObjectId::unserialize</a> — Unserialize an ObjectId</li></ul> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb-bson-minkey.unserialize.html">MongoDB\BSON\MinKey::unserialize</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb-bson-objectid.construct.html">MongoDB\BSON\ObjectId::__construct</a></div> <div class="up"><a href="book.bson.html">MongoDB\BSON</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>