php實(shí)現(xiàn)Mongodb自定義方式生成自增ID的方法_PHP教程
Jul 13, 2016 am 10:00 AMphp實(shí)現(xiàn)Mongodb自定義方式生成自增ID的方法
?這篇文章主要介紹了php實(shí)現(xiàn)Mongodb自定義方式生成自增ID的方法,實(shí)例分析了Mongodb自增字段的實(shí)現(xiàn)技巧與對(duì)應(yīng)php操作方法,需要的朋友可以參考下
?
本文實(shí)例講述了php實(shí)現(xiàn)Mongodb自定義方式生成自增ID的方法。分享給大家供大家參考。具體分析如下:
代碼如下:
>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 }
//注:因?yàn)閒indAndModify是一個(gè)方法完成更新查找兩個(gè)操作,所以具有原子性,多線程不會(huì)沖突。
//然后保存相應(yīng)的數(shù)據(jù)
>db.user.save({uid:userid.id, username:"kekeles", password:"kekeles", info:"http://www.jb51.net/ "});
//查看結(jié)果
> db.user.find();
{ "_id" : ObjectId("4c637f79900f00000000686d"), "uid" : 1, "username" : "admin", "password" : "admin" }
//這是mongo的shell,如果用的是服務(wù)器端程序java php python,可以自己對(duì)這些操作封裝一下,只用傳幾個(gè)參數(shù)就可以返回自增的id,還可以實(shí)現(xiàn)像Oracle的跨表的自增id。
?
自己寫了一段php的,拿出來(lái)給大家分享。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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(); ?> |
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











PHP ??? ??? ?? ??? ????? php.ini? ???? ? ??? ???? ??? ??? ????? ????. 1. PHP ?? : Linux ? APT, Mac ? Homebrew ? Windows? ???? XAMPP ??; 2. php.ini ?? : ?? ???, ??? ?? ?? ???? ??? ?? ?????. 3. ? ?? ?? : Apache? mod_php? ???? nginx? php-fpm? ?????. 4. ?? ??? ???? ?? ????? ???? ?? : MySQLI, JSON, MBString ?? ?? ??.

PHP ??? ???? ??? ??? ??? ????? ????. ??? ????? ?? ???? ??? "?? ? ?"??? "?"? ???????. 1. ??? ? ??? ??? DocBlock (/*/)? ?? ?? ??? ???? ??? ? ?? ???? ??????. 2. JS ??? ???? ?? ???? ??? ?? ??? ??? ?????. 3. ??? ?? ?? ?? ??? ???? ????? ????? ???? ?? ????? ???? ? ??????. 4. Todo ? Fixme? ????? ???? ? ? ??? ??? ???? ?? ?? ? ??? ???????. ??? ???? ?? ??? ??? ?? ?? ?? ???? ???? ? ????.

tolearnpheffectical, startBysetTupaloCalserErverEnmentUsingToolslikexamppandacodeeditor -likevscode.1) installxamppforapache, mysql, andphp.2) useacodeeditorforsyntaxsupport.3)) 3) testimplephpfile.next, withpluclucincludechlucincluclucludechluclucled

?? ??????? ??? ?? ?? ??? ?? ????? ??? ???? ??? ????? ??? ??? ??? ??? ? ? ????. ??? ??? ??? ?? ???? ???? ?? ? ? ????. ??? ????? ???? ??? ??? ??, ?? ?? ?? ?? ? ?? ?? ??? ?????. ??? ?????? ???? ??? ??? ???? ? ? ??? ????? ??? ???? ????. ?? ?? ??? ???? ??, ??? ? ??? ?? ???? ?? ?? ? ?? ?? ???? ??? ????????. ??? ??? ?? ??? ??? ???? ?? ??? ?? ? ? ????. ??? ??? ???? ?? ??? ???? ?? ?? ?? ??? ??????.

phpblockommentsearseforwritingmulti-lleexplanations, temporlyblingcode, and generatingdocumentation.theyshouldnotbenesteTeRleftUnclosed.blockmentShelPindOcumentingFunctionSwitHphPDoc, whatlsoompsTormuseforauto-CompletionAnderRorChe

toinstallphpquickly, usexampponwindowsorhomebrewonmacos.1. ??, downloadandinstallxAmpp, selectComponents, startApache ? placefilesinhtdocs.2

?? ??? ???? ??? ??? ???? ????? ?? "?? ? ?"? ??? "?"? ???? ????. 1. ??? ?? ?? ?? ??? ?? ??? ?? ??? ??? ???????. 2. ??? ??? ?? ?? ??? ???? ?? ?? ????? ???? ????? ?????. 3. ????? ??? ???? ??? ???? ????, ???? ???, ??? ?? ?? ???? ?????. 4. ??? ?? ? ? ??? ??? ???? ?? ??? ??? ??? ?? ??? ?? ?? ??? ??????.

PHP ?? ??? ??? ???? ???? ?????. 1. ??? ??? ??? ??? ??? ??? ???? ?? ?? ?? ??? ??????? ??? ??? ???????. 2. ???? ????? ?? ?? ?, ?? ?? ???, API ????? ?? ?? ?? ????? ??? ?????. 3. ?? ?? ???? ??? ???? ????? ???? PHPDOC? ?? ?? ??? ??????. 4. ??? ???? ???? ?? ??? ????? ??????????. ?? ??? ?? ??? ???? ???? ?? ??? ??? ?? ?? ????? ?????????.
