Currently, the PHP driver does not have built-in functions for this, __set_state (), which is mentioned by another answer, only in order to be able to deserialize the ID for the session and does not allow creating it through certain components.
To automatically create an identifier, you will need to do the following:
<?php function createId( $yourTimestamp ) { static $inc = 0; $ts = pack( 'N', $yourTimestamp ); $m = substr( md5( gethostname()), 0, 3 ); $pid = pack( 'n', posix_getpid() ); $trail = substr( pack( 'N', $inc++ ), 1, 3); $bin = sprintf("%s%s%s%s", $ts, $m, $pid, $trail); $id = ''; for ($i = 0; $i < 12; $i++ ) { $id .= sprintf("%02X", ord($bin[$i])); } return new MongoID($id); } var_dump( createId( time() ) ); ?>
source share