Created
October 20, 2015 06:11
-
-
Save betapcode/a3f6c122c9c40188a6a9 to your computer and use it in GitHub Desktop.
Auto incrêmnting Sequence in mongodb using php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$m = new MongoClient(); | |
// select a database | |
$db = $m->seq; | |
// select a collection (analogous to a relational database's table) | |
$collection = $db->counters; | |
$user_collection = $db->user; | |
/**********Function to auto increment seq************/ | |
function getNextSequence($name){ | |
global $collection; | |
$retval = $collection->findAndModify( | |
array('_id' => $name), | |
array('$inc' => array("seq" => 1)), | |
null, | |
array( | |
"new" => true, | |
) | |
); | |
return $retval['seq']; | |
} | |
$db_array=array('_id' => getNextSequence("userid"), 'name' => 'debojit'); | |
$user_collection->insert($db_array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment