<!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 MongoId class</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.types.html">Types</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongoid.construct.html">MongoId::__construct</a></div> <div class="up"><a href="mongo.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="class.mongoid" class="reference"> <h1 class="title">The MongoId class</h1> <div class="partintro"><p class="verinfo">(PECL mongo >=0.8.0)</p> <div class="section" id="mongoid.intro"> <h2 class="title">Introduction</h2> <p class="para"> A unique identifier created for database objects. If an object is inserted into the database without an _id field, an _id field will be added to it with a <strong class="classname">MongoId</strong> instance as its value. If the data has a naturally occuring unique field (say, a username or timestamp) it is fine to use this as the _id field instead, and it will not be replaced with a <strong class="classname">MongoId</strong>. </p> <p class="para"> Instances of the <strong class="classname">MongoId</strong> class fulfill the role that autoincrementing does in a relational database: to provide a unique key if the data does not natually have one. Autoincrementing does not work well with a sharded database, as it is impossible to find what the next number should be quickly. This class fulfills the constraints of quickly generating a value that is unique across shards. </p> <p class="para"> Each MongoId is 12 bytes (making its string form 24 hexidecimal characters). The first four bytes are a timestamp, the next three are a hash of the client machine's hostname, the next two are the two least significant bytes of the process id running the script, and the last three bytes are an incrementing value. </p> <p class="para"> <strong class="classname">MongoId</strong>s are serializable/unserializable. Their serialized form is similar to their string form: <div class="example-contents screen"> <div class="cdata"><pre> C:7:"MongoId":24:{4af9f23d8ead0e1d32000000} </pre></div> </div> </p> </div> <div class="section" id="mongoid.synopsis"> <h2 class="title">Class synopsis</h2> <div class="classsynopsis"> <div class="ooclass"></div> <div class="classsynopsisinfo"> <span class="ooclass"> <strong class="classname">MongoId</strong> </span> {</div> <div class="fieldsynopsis"> <span class="modifier">public</span> <span class="type">string</span> <var class="varname"><a href="class.mongoid.html#mongoid.props.id">$<var class="varname">id</var></a></var> <span class="initializer"> = <strong><code>NULL</code></strong></span> ;</div> <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="methodname"><a href="mongoid.construct.html" class="methodname">__construct</a></span> ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$id</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span> ] )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">string</span> <span class="methodname"><a href="mongoid.gethostname.html" class="methodname">getHostname</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">int</span> <span class="methodname"><a href="mongoid.getinc.html" class="methodname">getInc</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">int</span> <span class="methodname"><a href="mongoid.getpid.html" class="methodname">getPID</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">int</span> <span class="methodname"><a href="mongoid.gettimestamp.html" class="methodname">getTimestamp</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">MongoId</span> <span class="methodname"><a href="mongoid.set-state.html" class="methodname">__set_state</a></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$props</code></span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><a href="mongoid.tostring.html" class="methodname">__toString</a></span> ( <span class="methodparam">void</span> )</div> }</div> </div> <div class="section"> <h2 class="title">Fields</h2> <dl> <dt id="mongoid.props.id"> <span class="term"><var class="varname"><var class="varname">id</var></var></span> <dd> <span class="simpara"> This field contains the string representation of this object. </span> </dd> </dt> </dl> </div> <div class="section"> <h2 class="title">See Also</h2> <p class="para"> MongoDB core docs on <a href="http://docs.mongodb.org/manual/reference/object-id/" class="link external">» ids</a>. </p> </div> </div> <h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="mongoid.construct.html">MongoId::__construct</a> — Creates a new id</li><li><a href="mongoid.gethostname.html">MongoId::getHostname</a> — Gets the hostname being used for this machine's ids</li><li><a href="mongoid.getinc.html">MongoId::getInc</a> — Gets the incremented value to create this id</li><li><a href="mongoid.getpid.html">MongoId::getPID</a> — Gets the process ID</li><li><a href="mongoid.gettimestamp.html">MongoId::getTimestamp</a> — Gets the number of seconds since the epoch that this id was created</li><li><a href="mongoid.set-state.html">MongoId::__set_state</a> — Create a dummy MongoId</li><li><a href="mongoid.tostring.html">MongoId::__toString</a> — Returns a hexidecimal representation of this id</li></ul> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.types.html">Types</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongoid.construct.html">MongoId::__construct</a></div> <div class="up"><a href="mongo.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>