PHP7 mongoDB扩展使用的方法分享

这篇文章主要给大家介绍了关于PHP7 mongoDB扩展使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用PHP7具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。

前言

最近在做的项目需要将PHP5.6升级到PHP7.0,使用过PHP-mongo扩展的同学应该知道,PHP7.0的mongodb扩展是完全不兼容PHP5.6的mongo扩展的,php-mongodb改如何使用呢。

下面直接说明各种方法的使用:

1.mongodb连接:

  1. private function connect($confArr) {
  2. try{
  3. $connStr = "mongodb://" . $confArr['host'] . ":" . $confArr['port'] . "/" . $confArr['db_name'];
  4. $options = array(
  5. 'username' => $confArr['username'],
  6. 'password' => $confArr['password'],
  7. 'readPreference' => $confArr['read_preference'],
  8. 'connectTimeoutMS' => intval($confArr['connect_timeout_ms']),
  9. 'socketTimeoutMS' => intval($confArr['socket_timeout_ms']),
  10. );
  11. $mc = new MongoDB\Driver\Manager($connStr, $options);
  12. return $mc;
  13. }
  14. catch(Exception $e){
  15. return false;
  16. }
  17. }

2.查询find:

  1. public function find($query = array(), $fields = array(), $collection, $sort = array(), $limit = 0, $skip = 0) {
  2. $conn = $this->connect();
  3. if (emptyempty($conn)) {
  4. return false;
  5. }
  6. try {
  7. $data = array();
  8. $options = array();
  9. if (!emptyempty($query)) {
  10. $options['projection'] = array_fill_keys($fields, 1);
  11. }
  12. if (!emptyempty($sort)) {
  13. $options['sort'] = $sort;
  14. }
  15. if (!emptyempty($limit)) {
  16. $options['skip'] = $skip;
  17. $options['limit'] = $limit;
  18. }
  19. $mongoQuery = new MongoDB\Driver\Query($query, $options);
  20. $readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
  21. $cursor = $conn->executeQuery($collection, $mongoQuery, $readPreference);
  22. foreach($cursor as $value) {
  23. $data[] = (array)$value;
  24. }
  25. return $data;
  26. } catch (Exception $e) {
  27. //记录错误日志
  28. }
  29. return false;
  30. }

3.插入操作insert:

  1. public function insert($addArr, $collection) {
  2. if (emptyempty($addArr) || !is_array($addArr)) {
  3. return false;
  4. }
  5. $conn = $this->connect();
  6. if (emptyempty($conn)) {
  7. return false;
  8. }
  9. try {
  10. $bulk = new MongoDB\Driver\BulkWrite();
  11. $bulk->insert($addArr);
  12. $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 6000);
  13. $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern);
  14. if ($result->getInsertedCount()) {
  15. return true;
  16. }
  17. } catch (Exception $e) {
  18. //记录错误日志
  19. }
  20. return false;
  21. }

4.删除delete:

  1. public function delete($whereArr, $options = array(), $collection) {
  2. if (emptyempty($whereArr)) {
  3. return false;
  4. }
  5. if (!isset($options['justOne'])) {
  6. $options = array(
  7. 'justOne' => false,
  8. );
  9. }
  10. $conn = $this->connect();
  11. if (emptyempty($conn)) {
  12. return false;
  13. }
  14. try {
  15. $bulk = new MongoDB\Driver\BulkWrite();
  16. $bulk->delete($whereArr, $options);
  17. $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 30000);
  18. $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern);
  19. return true;
  20. } catch (Exception $e) {
  21. //记录错误日志
  22. }
  23. return false;
  24. }

5.执行command操作:

  1. private function command($params, $dbName) {
  2. $conn = $this->connect();
  3. if (emptyempty($conn)) {
  4. return false;
  5. }
  6. try {
  7. $cmd = new MongoDB\Driver\Command($params);
  8. $result = $conn->executeCommand($dbName, $cmd);
  9. return $result;
  10. } catch (Exception $e) {
  11. //记录错误
  12. }
  13. return false;
  14. }

6.统计count:

  1. public function count($query, $collection) {
  2. try {
  3. $cmd = array(
  4. 'count' => $collection,
  5. 'query' => $query,
  6. );
  7. $res = $this->command($cmd);
  8. $result = $res->toArray();
  9. return $result[0]->n;
  10. } catch (Exception $e) {
  11. //记录错误
  12. }
  13. return false;
  14. }

7.聚合distinct:

  1. public function distinct($key, $where, $collection) {
  2. try {
  3. $cmd = array(
  4. 'distinct' => $collection,
  5. 'key' => $key,
  6. 'query' => $where,
  7. );
  8. $res = $this->command($cmd);
  9. $result = $res->toArray();
  10. return $result[0]->values;
  11. } catch (Exception $e) {
  12. //记录错误
  13. }
  14. return false;
  15. }

8.aggregate操作:

  1. public function aggregate($where, $group, $collection) {
  2. try {
  3. $cmd = array(
  4. 'aggregate' => $collection,
  5. 'pipeline' => array(
  6. array(
  7. '$match' => $where,
  8. ),
  9. array(
  10. '$group' => $group,
  11. ),
  12. ),
  13. 'explain' => false,
  14. );
  15. $res = $this->command($cmd);
  16. if (!$res) {
  17. return false;
  18. }
  19. $result = $res->toArray();
  20. return $result[0]->total;
  21. } catch (Exception $e) {
  22. //记录错误
  23. }
  24. return false;
  25. }