MongoDB PHP auto Increment self-increment

Source: Internet
Author: User
Tags postgresql vars

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?
  1. > Db.counters.insert ( //Counter table
  2. {
  3. _id: "userid",
  4. seq:0
  5. }
  6. );
  7. Writeresult ({ "ninserted": 1})
  8. > Db.counters.find ();
  9. { "_id": "userid", "seq": 0}
  10. > function getnextsequence (name) { //Remove ID functions
  11. var ret = db.counters.findAndModify (
  12. {
  13. Query: {_id:name},
  14. Update: { $inc: {seq:1}}, //here seq is the SEQ field in the Counters table above
  15. New:true,
  16. Upsert:true
  17. }
  18. );
  19. return ret.seq;
  20. };
  21. > Db.users.insert ( //Insert Data
  22. {
  23. _id:getnextsequence ("userid"),
  24. Name: "Tank"
  25. }
  26. );
  27. Writeresult ({ "ninserted": 1})
  28. > Db.users.find (); //View
  29. { "_id": 1, "name": "Tank"}
  30. > Db.users.insert (
  31. {
  32. _id:getnextsequence ("userid"),
  33. Name: "Test"
  34. }
  35. );
  36. Writeresult ({ "ninserted": 1})
  37. > Db.users.find ();
  38. { "_id": 1, "name": "Tank"}
  39. { "_id": 2, "name": "Test"}

2,php Implementation Auto_increment

View copy print?
  1. function Getnextid ($mongo,$name,$param =Array ()) {
  2. $param + = Array ( //default ID starting from 1, interval is 1
  3. ' init ' = 1,
  4. ' Step ' = 1,
  5. );
  6. $update = Array ('$inc ' = =Array (' id ' = ' = '$param [' Step ']); //Set interval
  7. $query = Array (' name ' = =$name);
  8. $command = Array (
  9. ' findandmodify ' = ' IDs ',
  10. ' Update ' = $update,
  11. ' query ' = $query,
  12. ' new ' = True
  13. );
  14. $id = $mongo->db->command ($command);
  15. if (isset ($id [' value '] [' ID '])) {
  16. return $id [' value '] [' id '];
  17. }else{
  18. $mongo->insert (array (
  19. ' name ' = $name,
  20. ' id ' = = $param [' init '], //Set ID start value
  21. ));
  22. return $param [' init '];
  23. }
  24. }
  25. $mongo = new MONGO ();
  26. $curDB = $mongo->selectcollection (' test ', ' IDs '); IDs tables in the//test library
  27. $user = $mongo->selectcollection (' test ', ' users '); users table in the//test library
  28. $id = Getnextid ($curDB,' userid ',array (' init ' =>10000,' Step ' =>2)); //Get the ID of the next piece of data
  29. $obj = Array ("_id" and "=" =$id,"name" = "Tankzhang");
  30. $user->insert ($obj); //Insert Data

MongoDB PHP auto Increment self-increment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.