zoukankan      html  css  js  c++  java
  • mongodb php auto increment 自增

     

    mongodb的自增实现根oracle,postgresql是差不多,都是通过计数器来实现的.

    oracle自增实现: 实例说明oracle序列用法

    postgresql自增实现: postgresql auto_increment 实现 通用方法

    1,mongodb命令行下实现auto_increment

    1. > db.counters.insert(             //计数器表  
    2.        {  
    3.             _id: "userid",  
    4.             seq: 0  
    5.        }  
    6.     );  
    7. WriteResult({ "nInserted" : 1 })  
    8.   
    9. > db.counters.find();  
    10. "_id" : "userid", "seq" : 0 }  
    11.   
    12. function getNextSequence(name) {      //取下个ID的函数  
    13.    var ret = db.counters.findAndModify(  
    14.        {  
    15.            query: { _id: name },  
    16.            update: { $inc: { seq: 1 } },  //这里seq就是上面counters表中的seq字段  
    17.            new: true,  
    18.            upsert: true  
    19.         }  
    20.    );  
    21.   
    22.    return ret.seq;  
    23.  };  
    24.   
    25. > db.users.insert(       //插入数据  
    26.       {  
    27.            _id: getNextSequence("userid"),  
    28.            name: "tank"  
    29.       }  
    30.  );  
    31. WriteResult({ "nInserted" : 1 })  
    32.   
    33. > db.users.find();     //查看  
    34. "_id" : 1, "name" : "tank" }  
    35.   
    36. > db.users.insert(  
    37.       {  
    38.            _id: getNextSequence("userid"),  
    39.            name: "test"  
    40.       }  
    41.  );  
    42. WriteResult({ "nInserted" : 1 })  
    43.   
    44. > db.users.find();  
    45. "_id" : 1, "name" : "tank" }  
    46. "_id" : 2, "name" : "test" }  

    2,php实现auto_increment

    1. function getNextId($mongo,$name,$param=array()){  
    2.   
    3.      $param += array(   //默认ID从1开始,间隔是1  
    4.        'init' => 1,  
    5.        'step' => 1,  
    6.      );  
    7.   
    8.      $update = array('$inc'=>array('id'=>$param['step']));   //设置间隔  
    9.      $query = array('name'=>$name);  
    10.      $command = array(  
    11.         'findandmodify' => 'ids',  
    12.         'update' => $update,  
    13.         'query' => $query,  
    14.         'new' => true  
    15.      );  
    16.   
    17.      $id = $mongo->db->command($command);  
    18.      if (isset($id['value']['id'])) {  
    19.         return $id['value']['id'];  
    20.      }else{  
    21.         $mongo->insert(array(  
    22.            'name' => $name,  
    23.            'id' => $param['init'],     //设置ID起始数值  
    24.         ));  
    25.         return $param['init'];  
    26.     }  
    27. }   
    28.   
    29. $mongo = new Mongo();  
    30. $curDB = $mongo->selectCollection('test', 'ids');     //test库中的ids表  
    31. $user = $mongo->selectCollection('test', 'users');      //test库中的users表  
    32.   
    33. $id = getNextId($curDB,'userid',array('init'=>10000,'step'=>2));   //取得下一条数据的ID  
    34.   
    35. $obj = array("_id"=>$id,"name"=>"tankzhang");  
    36. $user->insert($obj);   //插入数据  
  • 相关阅读:
    September 29th 2017 Week 39th Friday
    September 28th 2017 Week 39th Thursday
    September 27th 2017 Week 39th Wednesday
    September 26th 2017 Week 39th Tuesday
    September 25th 2017 Week 39th Monday
    September 24th 2017 Week 39th Sunday
    angular2 学习笔记 ( Form 表单 )
    angular2 学习笔记 ( Component 组件)
    angular2 学习笔记 ( Http 请求)
    angular2 学习笔记 ( Router 路由 )
  • 原文地址:https://www.cnblogs.com/shijiaoyun/p/5176915.html
Copyright © 2011-2022 走看看