MongoDB's self-increasing implementation of root oracle,postgresql is almost, all through the counter to achieve.
Oracle Self-increment implementation: example illustrates Oracle sequence usage
PostgreSQL self-increment implementation: PostgreSQL Auto_increment implements a common approach
1,MONGODB command-line implementation of Auto_increment
View copy print?
- > Db.counters.insert ( //Counter table
- {
- _id: "userid",
- seq:0
- }
- );
- Writeresult ({ "ninserted": 1})
- > Db.counters.find ();
- { "_id": "userid", "seq": 0}
- > function getnextsequence (name) { //Remove ID functions
- var ret = db.counters.findAndModify (
- {
- Query: {_id:name},
- Update: { $inc: {seq:1}}, //here seq is the SEQ field in the Counters table above
- New:true,
- Upsert:true
- }
- );
- return ret.seq;
- };
- > Db.users.insert ( //Insert Data
- {
- _id:getnextsequence ("userid"),
- Name: "Tank"
- }
- );
- Writeresult ({ "ninserted": 1})
- > Db.users.find (); //View
- { "_id": 1, "name": "Tank"}
- > Db.users.insert (
- {
- _id:getnextsequence ("userid"),
- Name: "Test"
- }
- );
- Writeresult ({ "ninserted": 1})
- > Db.users.find ();
- { "_id": 1, "name": "Tank"}
- { "_id": 2, "name": "Test"}
2,php Implementation Auto_increment
View copy print?
- function Getnextid ($mongo,$name,$param =Array ()) {
- $param + = Array ( //default ID starting from 1, interval is 1
- ' init ' = 1,
- ' Step ' = 1,
- );
- $update = Array ('$inc ' = =Array (' id ' = ' = '$param [' Step ']); //Set interval
- $query = Array (' name ' = =$name);
- $command = Array (
- ' findandmodify ' = ' IDs ',
- ' Update ' = $update,
- ' query ' = $query,
- ' new ' = True
- );
- $id = $mongo->db->command ($command);
- if (isset ($id [' value '] [' ID '])) {
- return $id [' value '] [' id '];
- }else{
- $mongo->insert (array (
- ' name ' = $name,
- ' id ' = = $param [' init '], //Set ID start value
- ));
- return $param [' init '];
- }
- }
- $mongo = new MONGO ();
- $curDB = $mongo->selectcollection (' test ', ' IDs '); IDs tables in the//test library
- $user = $mongo->selectcollection (' test ', ' users '); users table in the//test library
- $id = Getnextid ($curDB,' userid ',array (' init ' =>10000,' Step ' =>2)); //Get the ID of the next piece of data
- $obj = Array ("_id" and "=" =$id,"name" = "Tankzhang");
- $user->insert ($obj); //Insert Data
MongoDB PHP auto Increment self-increment