MongoDB Database common operation PHP code

Source: Internet
Author: User
  1. $conn = new Mongo ();
  2. can be shortened to
  3. $conn =new Mongo (); #连接本地主机, the default port.
  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. #选择集合 (select "Table")
  13. $collection = $db->user;
  14. $collection = $db->selectcollection ("user"); #第二种写法
  15. #插入操作
  16. $data =array ("uid" = "zz123", "user_name" = "Zhang San");
  17. $result = $collection->insert ($data); #简单插入
  18. echo "Insert Data ID". $data ["_id"];
  19. Exit
  20. #插入操作 Safe Insertion
  21. $data =array ("uid" = "zz124", "user_name" = "John Doe");
  22. $result = $collection->insert ($data, true); #用于等待MongoDB完成操作 in order to determine whether it was successful. (This parameter is useful when you have a large number of records inserted)
  23. #修改操作
  24. $where =array ("uid" = "zz123");
  25. $newdata =array ("user_name" = "Zhang 33", "tel" and "123456789");
  26. $result = $collection->update ($where, Array (' $set ' = = $newdata));
  27. #替换更新
  28. $where =array ("uid" = "zz124");
  29. $newdata =array ("User_age" and "a", "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 '));//Delete node Tel
  42. #删除数据
  43. $collection->remove (Array (' uid ' = ' zz124 '));
  44. #删除指定MongoId
  45. $id = new MongoId (' 4d638ea1d549a02801000011 ');
  46. $collection->remove (Array (' _id ' = = (object) $id));
  47. #查询数据 Note: $GT is greater than, $GTE is greater than or equal, $lt is less than, $lte is less than or equal, $ne is not equal, $exists does not exist
  48. Echo ' Count: '. $collection->count (). "
    "; #全部
  49. Echo ' Count: '. $collection->count (' uid ' = ' zz123 '). "
    "; #可以加上条件
  50. Echo ' Count: '. $collection->count (' 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 to not show
  61. $cursor = $collection->find ()->fields (Array ("Age" =>false, "tel" =>false));
  62. #指定列 true to display
  63. $cursor = $collection->find ()->fields (Array ("user_name" =>true));
  64. # (exists tel,age node) 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 means ascending, and the order of the arguments affects the sort sequence
  71. #索引
  72. $collection->ensureindex (Array (' age ' = 1, ' money ' =>-1)); #1表示降序-1 means ascending
  73. $collection->ensureindex (Array (' age ' = 1, ' money ' =>-1), array (' background ' =>true)); #索引的创建放在后台运行 (default is run synchronously)
  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. ?>
Copy Code
MongoDB, PHP
  • 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.