The code is roughly as follows:
Copy CodeThe code is as follows:
function generate_auto_increment_id ($namespace, array $option = Array ())
{
$option + = Array (
' Init ' = 1,
' Step ' = 1,
);
$instance = new Mongo ();
$instance = $instance->selectcollection (' _seq ', ' seq ');
$seq = $instance->db->command (Array (
' findandmodify ' = ' seq ',
' Query ' = = Array (' _id ' = $namespace),
' Update ' = Array (' $inc ' = = Array (' id ' = ' = ' $option [' Step ']),
' New ' = true,
));
if (Isset ($seq [' Value '] [' ID '])) {
return $seq [' value '] [' id '];
}
$instance->insert (Array (
' _id ' = $namespace,
' id ' = $option [' init '],
));
return $option [' init '];
}
Var_dump (generate_auto_increment_id (' foo '));
Var_dump (generate_auto_increment_id (' Bar ', Array (' init ' = 123));
?>
Its implementation is mainly the use of MongoDB in the Findandmodify command, as long as every time into MongoDB insert object before the generation of ID assignment to _id OK, because its implementation to meet the atomicity, so there is no concurrency problem.
It is also stated that the findandmodify itself provides a upsert parameter, true if it can be automatically insert, but then cannot customize the initial value, so the example in this article does not use Upsert.
BTW, the name of the database "_seq" begins with an underscore, so the list is lined up in front and easier to distinguish.
Reference: Auto Increment with MongoDB
http://www.bkjia.com/PHPjc/323090.html www.bkjia.com true http://www.bkjia.com/PHPjc/323090.html techarticle The code looks like this: the copy code code is as follows:? PHP function generate_auto_increment_id ($namespace, array $option = Array ()) {$option + = array ( ' Init ' = 1, ' Step ' = 1, '; $...