PHP to implement MongoDB custom way to generate self-increasing ID method _php Tips

Source: Internet
Author: User
Tags mongodb

This article describes the way PHP implements MongoDB customization to generate the self-increasing ID. Share to everyone for your reference. The specific analysis is as follows:

Copy Code code as follows:
First create an automatic growth ID collection IDs
>db.ids.save ({name: "User", id:0});
You can see if it's successful.
> Db.ids.find ();
{"_id": ObjectId ("4c637dbd900f00000000686c"), "name": "User", "id": 0}
And then add the IDs collection to get the IDs each time before adding new users
>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 that completes the update lookup for two operations, it is atomic and multithreading does not conflict.
And then save the appropriate data
>db.user.save ({uid:userid.id, username: "Kekeles", Password: "Kekeles", Info: "http://www.jb51.net/"});
View Results
> Db.user.find ();
{"_id": ObjectId ("4c637f79900f00000000686d"), "UID": 1, "username": "admin", "password": "Admin"}
This is the shell of the MONGO, if you are using the server-side Java PHP python, you can encapsulate these operations, using only a few parameters to return the ID, you can also implement a cross table like Oracle ID.

I wrote a section of PHP, take out to share.

<?php
function 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 (Array (
' uid ' => $id, 
' username ' => ' kekeles ', ' Password ' => ' kekeles ') 
,
 ' info ' => ' http://www.jb51.net/')
);
$conn->close ();
? >

I hope this article will help you with your PHP program design.

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.