MongoDB資料庫常用操作PHP代碼

來源:互聯網
上載者:User
  1. $conn = new Mongo();
  2. //可以簡寫為
  3. //$conn=new Mongo(); #串連本地主機,預設連接埠.
  4. //$conn=new Mongo(“172.21.15.69″); #串連遠程主機
  5. //$conn=new Mongo(“xiaocai.loc:10086″); #串連指定連接埠遠程主機
  6. //$conn=new Mongo(“xiaocai.loc”,array(“replicaSet”=>true)); #負載平衡
  7. //$conn=new Mongo(“xiaocai.loc”,array(“persist”=>”t”)); #持久串連
  8. //$conn=new Mongo(“mongodb://sa:123@localhost”); #帶使用者名稱密碼
  9. #選擇test資料庫
  10. $db=$conn->test;
  11. //$db=$conn->selectDB("test"); #第二種寫法
  12. #選擇集合(選擇"表")
  13. $collection=$db->user;
  14. //$collection=$db->selectCollection("user"); #第二種寫法
  15. #插入操作
  16. $data=array("uid"=>"zz123","user_name"=>"張三");
  17. $result=$collection->insert($data); #簡單插入
  18. echo "插入資料的id".$data["_id"];
  19. exit;
  20. #插入操作 安全插入
  21. $data=array("uid"=>"zz124","user_name"=>"李四");
  22. $result=$collection->insert($data,true); #用於等待MongoDB完成操作,以便確定是否成功.(當有大量記錄插入時使用該參數會比較有用)
  23. #修改操作
  24. $where=array("uid"=>"zz123");
  25. $newdata=array("user_name"=>"張三三","tel"=>"123456789");
  26. $result=$collection->update($where,array('$set'=>$newdata));
  27. #替換更新
  28. $where=array("uid"=>"zz124");
  29. $newdata=array("user_age"=>"22","tel"=>"123456789");
  30. $result=$collection->update($where,$newdata);
  31. #批次更新
  32. $where=array("uid"=>'zz');
  33. $newdata=array("user_name"=>"zz","money"=>1000);
  34. $result=$collection->update($where,array('$set'=>$newdata),array('multiple'=>true));
  35. #自動累加
  36. $where=array('money'=>1000);
  37. $newdata=array('user_name'=>'edit');
  38. $result=$collection->update($where,array('$set'=>$newdata,'$inc'=>array('money'=>-5)));
  39. #刪除節點
  40. $where=array('uid'=>'zz124');
  41. $result=$collection->update($where,array('$unset'=>'tel'));//刪除節點tel
  42. #刪除資料
  43. $collection->remove(array('uid'=>'zz124'));
  44. #刪除指定MongoId
  45. $id = new MongoId('4d638ea1d549a02801000011');
  46. $collection->remove(array('_id'=>(object)$id));
  47. #查詢資料 注:$gt為大於、$gte為大於等於、$lt為小於、$lte為小於等於、$ne為不等於、$exists不存在
  48. echo 'count:'.$collection->count()."
    "; #全部
  49. echo 'count:'.$collection->count(array('uid'=>'zz123'))."
    "; #可以加上條件
  50. echo 'count:'.$collection->count(array('age'=>array('$gt'=>10,'$lte'=>30)))."
    "; #大於50小於等於74
  51. echo 'count:'.$collection->find()->limit(5)->skip(0)->count(true)."
    "; #獲得實際返回的結果數
  52. #集合中所有文檔
  53. $cursor = $collection->find()->snapshot();
  54. foreach ($cursor as $id => $value) {
  55. echo "$id: "; var_dump($value);
  56. echo "
    ";
  57. }
  58. #查詢一條資料
  59. $cursor = $collection->findOne();
  60. #排除列 false為不顯示
  61. $cursor = $collection->find()->fields(array("age"=>false,"tel"=>false));
  62. #指定列 true為顯示
  63. $cursor = $collection->find()->fields(array("user_name"=>true));
  64. #(存在tel,age節點) and age!=0 and age<50
  65. $where=array('tel'=>array('$exists'=>true),'age'=>array('$ne'=>0,'$lt'=>50,'$exists'=>true));
  66. $cursor = $collection->find($where);
  67. #分頁擷取結果集
  68. $cursor = $collection->find()->limit(5)->skip(0);
  69. #排序
  70. $cursor = $collection->find()->sort(array('age'=>-1,'type'=>1)); #1表示降序 -1表示升序,參數的先後影響排序次序
  71. #索引
  72. $collection->ensureIndex(array('age' => 1,'money'=>-1)); #1表示降序 -1表示升序
  73. $collection->ensureIndex(array('age' => 1,'money'=>-1),array('background'=>true)); #索引的建立放在後台運行(預設是同步運行)
  74. $collection->ensureIndex(array('age' => 1,'money'=>-1),array('unique'=>true)); #該索引是唯一的
  75. #取得查詢結果
  76. $cursor = $collection->find();
  77. $array=array();
  78. foreach ($cursor as $id => $value) {
  79. $array[]=$value;
  80. }
  81. #關閉串連
  82. $conn->close();
  83. ?>
複製代碼
MongoDB, PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.