PHP implements MongoDB custom method to generate the self-increment ID, mongodbid
The example in this paper describes how to generate a self-increment ID by implementing the MongoDB customization method in PHP. Share to everyone for your reference. The specific analysis is as follows:
Copy the code as follows://First create an auto-Grow ID collection IDs
>db.ids.save ({name: "User", id:0});
Can see if it's successful
> Db.ids.find ();
{"_id": ObjectId ("4c637dbd900f00000000686c"), "name": "User", "id": 0}
Then each time you add a new user, increase the IDs collection to get the ID
>userid = db.ids.findAndModify ({update:{$inc: {' id ': 1}}, query:{"name": "User"}, new:true});
{"_id": ObjectId ("4c637dbd900f00000000686c"), "name": "User", "id": 1}
Note: Because Findandmodify is a method to complete an update to find two operations, it has atomicity, and multithreading does not conflict.
Then save the appropriate data
>db.user.save ({uid:userid.id, username: "Kekeles", Password: "Kekeles", Info: "http://www.bkjia.com/"});
View Results
> Db.user.find ();
{"_id": ObjectId ("4c637f79900f00000000686d"), "UID": 1, "username": "admin", "password": "Admin"}
This is the shell of the MONGO, if you use a server-side program Java PHP python, you can encapsulate these operations, only a few parameters can return the self-increment ID, but also to achieve a cross-table like Oracle self-increment ID.
I wrote a piece of PHP, take it out for everyone to share.
<?phpfunction Mid ($name, $db) {$update = array (' $inc ' =>array ("id" =>1)), $query = Array (' name ' = = $name); $ Command = Array (' findandmodify ' = ' IDs ', ' update ' = $update, ' query ' = ' $query, ' new ' =>true, ' upsert ' = true); $id = $db->command ($command); return $id [' value '] [' ID '];} $conn = new Mongo (), $db = $conn->idtest; $id = Mid (' user ', $db), $db->user->save (' uid ' = ' $id, ' username ' = ' kekeles ', ' password ' = ' kekeles ', ' info ' = ' http://www.bkjia.com/'); $conn->close ();? >
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/972648.html www.bkjia.com true http://www.bkjia.com/PHPjc/972648.html techarticle PHP Implementation of the MongoDB custom way to generate the self-id method, Mongodbid The example of this article describes how to implement the MongoDB Custom mode of PHP to generate the self-increment ID method. Share to everyone for your reference. Specific ...