PHP implementation of MongoDB custom way to generate the self-increment ID method, mongodbid_php Tutorial

Source: Internet
Author: User

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 ...

  • 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.