PHP Doku:: The MongoId class - class.mongoid.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAnbieterspezifische DatenbankerweiterungenMongoDB Native DriverTypesThe MongoId class

Ein Service von Reinhard Neidl - Webprogrammierung.





The MongoId class


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 MongoId 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 MongoId.

Instances of the MongoId 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.

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.

MongoIds are serializable/unserializable. Their serialized form is similar to their string form:



MongoId {
public string $$id = NULL ;
/* Methoden */
__construct ([ string $id = NULL ] )
publicstaticstring getHostname ( void )
public int getInc ( void )
public int getPID ( void )
public int getTimestamp ( void )
publicstaticMongoId __set_state ( array $props )
public string __toString ( void )


This field contains the string representation of this object.

Siehe auch

MongoDB core docs on » ids.


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
sararschreiber at gmail dot com
12.10.2010 21:23
this is useful for querying for an object by id, given the id's hex:

= 4cb4ab6d7addf98506010000;

$theObjId = new MongoId($userid);

$connection = new Mongo();
$db = $connection->thedb->users;

// this will return our matching entry.
$item = $db->findOne(array("_id" => $theObjId));



PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)