search
Homephp教程PHP开发PHP implements Mongodb's custom method to generate self-increasing IDs

The example in this article describes how PHP implements Mongodb's custom method to generate self-increasing IDs. Share it with everyone for your reference. The specific analysis is as follows:

//首先创建一个自动增长id集合 ids
>db.ids.save({name:"user", id:0});
//可以查看一下是否成功
> db.ids.find();
{ "_id" : ObjectId("4c637dbd900f00000000686c"), "name" : "user", "id" : 0 }
//然后每次添加新用户之前自增一下 ids集合 获得id
>userid = db.ids.findAndModify({update:{$inc:{'id':1}}, query:{"name":"user"}, new:true});
{ "_id" : ObjectId("4c637dbd900f00000000686c"), "name" : "user", "id" : 1 }
//注:因为findAndModify是一个方法完成更新查找两个操作,所以具有原子性,多线程不会冲突。
//然后保存相应的数据
>db.user.save({uid:userid.id, username:"kekeles", password:"kekeles", info:"http://www.jb51.net/ "});
//查看结果
> db.user.find();
{ "_id" : ObjectId("4c637f79900f00000000686d"), "uid" : 1, "username" : "admin", "password" : "admin" }
//这是mongo的shell,如果用的是服务器端程序java php python,可以自己对这些操作封装一下,只用传几个参数就可以返回自增的id,还可以实现像Oracle的跨表的自增id。

I wrote a piece of php myself and shared it with everyone.

<?php
function mid($name, $db){
$update = array(&#39;$inc&#39;=>array("id"=>1));
$query = array(&#39;name&#39;=>$name);
$command = array(
&#39;findandmodify&#39;=>&#39;ids&#39;, &#39;update&#39;=>$update,
&#39;query&#39;=>$query, &#39;new&#39;=>true, &#39;upsert&#39;=>true
);
$id = $db->command($command);
return $id[&#39;value&#39;][&#39;id&#39;];
}
$conn = new Mongo();
$db = $conn->idtest;
$id = mid(&#39;user&#39;, $db);
$db->user->save(array(
&#39;uid&#39;=>$id, 
&#39;username&#39;=>&#39;kekeles&#39;, 
&#39;password&#39;=>&#39;kekeles&#39;,
 &#39;info&#39;=>&#39;http://www.jb51.net/ &#39;
));
$conn->close();
?>

I hope this article will be helpful to everyone’s PHP programming design.

For more articles on how to use PHP to implement Mongodb’s custom method to generate self-increasing IDs, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools