旧有的MongodbClient类已经被废弃了,现在PHP 连接mongo的时候使用新的MongoDB\Driver\Manager 来创建,下面是随便简单的封装了一个类,用来增删改查,只是实现了简单了,复杂的还是看文档比较靠谱Php_Mongodb - 图1
    Php_Mongodb - 图2

    1. <?php
    2. /**
    3. * Created by PhpStorm.
    4. * User: raye
    5. * Date: 17/11/30
    6. * Time: 19:00
    7. */
    8. use MongoDB\Driver\Manager;
    9. use MongoDB\Driver\Query;
    10. use MongoDB\Driver\BulkWrite;
    11. use MongoDB\Driver\ReadPreference;
    12. class MongodbManage {
    13. /** @var Manager $instance*/
    14. private static $instance;
    15. private static $_dbname;
    16. private $manager;
    17. private $_host = '192.168.1.83';
    18. private $_port = 27017;
    19. private $_bulk;
    20. private function __construct($dbname)
    21. {
    22. self::$_dbname = $dbname;
    23. $this->_bulk = new BulkWrite();
    24. $this->manager = new Manager('mongodb://' . $this->_host .':'. $this->_port . '/' . self::$_dbname);
    25. }
    26. public static function instance($dbname){
    27. if (self::$_dbname == $dbname && self::$instance instanceof self) {
    28. return self::$instance;
    29. }
    30. self::$instance = new self($dbname);
    31. return self::$instance;
    32. }
    33. public function insert($collection, array $data){
    34. if (empty($collection) || empty($data)) {
    35. throw new Exception('collection or insert data can not be null');
    36. }
    37. try{
    38. $this->_bulk->insert($data);
    39. return $this->manager->executeBulkWrite(self::$_dbname .'.'. $collection, $this->_bulk);
    40. }catch (Exception $e) {
    41. new Exception($e->getLine() . '--' . $e->getMessage());
    42. }
    43. }
    44. public function find($collection, array $filter = [], $returnArr = false, array $options = [], $readPreference = 'primary'){
    45. if (empty($collection)) {
    46. throw new Exception('collection can not be null');
    47. }
    48. $query = new Query($filter, $options);
    49. $res = $this->manager->executeQuery(self::$_dbname .'.'. $collection, $query, new ReadPreference($readPreference))->toArray();
    50. if (!$returnArr){
    51. return $res;
    52. }
    53. array_walk($res,function (&$value){
    54. $value = (array)$value;
    55. });
    56. return $res;
    57. }
    58. public function multiInsert($collection, array $data){
    59. if (empty($collection) || empty($data)) {
    60. throw new Exception('collection and data can not be empty');
    61. }
    62. foreach ($data as $value) {
    63. $this->_bulk->insert($value);
    64. }
    65. $res = $this->manager->executeBulkWrite(self::$_dbname .'.'. $collection, $this->_bulk);
    66. return $res and $res->getInsertedCount() > 0 ? true : false;
    67. }
    68. public function update($collection, $filter, array $data) {
    69. if (empty($collection) || empty($filter)) {
    70. throw new Exception('collection and filter can not be null');
    71. }
    72. $this->_bulk->update($filter, $data);
    73. $res = $this->manager->executeBulkWrite(self::$_dbname .'.'. $collection,$this->_bulk);
    74. return $res->getModifiedCount() > 0 ? true : false;
    75. }
    76. public function delete($collection, $filter, $options = []){
    77. if (empty($collection) || empty($filter)) {
    78. throw new Exception('collection and filter can not be null');
    79. }
    80. $this->_bulk->delete($filter, $options);
    81. $res = $this->manager->executeBulkWrite(self::$_dbname .'.'. $collection, $this->_bulk);
    82. return $res->getDeletedCount() > 0 ? true : false;
    83. }
    84. }