PHP7 mongoDB扩展使用的方法分享
网络编程 2021-07-04 23:24www.168986.cn编程入门
这篇文章主要给大家介绍了关于PHP7 mongoDB扩展使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用PHP7具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化来一起学习学习吧
前言
最近在做的项目需要将PHP5.6升级到PHP7.0,使用过PHP-mongo扩展的同学应该知道,PHP7.0的mongodb扩展是完全不兼容PHP5.6的mongo扩展的,php-mongodb改如何使用呢。
狼蚁网站SEO优化直接说明各种方法的使用:
1.mongodb连接
private function connect($confArr) { try{ $connStr = "mongodb://" . $confArr['host'] . ":" . $confArr['port'] . "/" . $confArr['db_name']; $options = array( 'username' => $confArr['username'], 'password' => $confArr['password'], 'readPreference' => $confArr['read_preference'], 'connectTimeoutMS' => intval($confArr['connect_timeout_ms']), 'socketTimeoutMS' => intval($confArr['socket_timeout_ms']), ); $mc = new MongoDB\Driver\Manager($connStr, $options); return $mc; } catch(Exception $e){ return false; } }
2.查询find:
public function find($query = array(), $fields = array(), $collection, $sort = array(), $limit = 0, $skip = 0) { $conn = $this->connect(); if (empty($conn)) { return false; } try { $data = array(); $options = array(); if (!empty($query)) { $options['projection'] = array_fill_keys($fields, 1); } if (!empty($sort)) { $options['sort'] = $sort; } if (!empty($limit)) { $options['skip'] = $skip; $options['limit'] = $limit; } $mongoQuery = new MongoDB\Driver\Query($query, $options); $readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY); $cursor = $conn->executeQuery($collection, $mongoQuery, $readPreference); foreach($cursor as $value) { $data[] = (array)$value; } return $data; } catch (Exception $e) { //记录错误日志 } return false; }
3.插入操作insert:
public function insert($addArr, $collection) { if (empty($addArr) || !is_array($addArr)) { return false; } $conn = $this->connect(); if (empty($conn)) { return false; } try { $bulk = new MongoDB\Driver\BulkWrite(); $bulk->insert($addArr); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 6000); $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern); if ($result->getInsertedCount()) { return true; } } catch (Exception $e) { //记录错误日志 } return false; }
4.删除delete
public function delete($whereArr, $options = array(), $collection) { if (empty($whereArr)) { return false; } if (!isset($options['justOne'])) { $options = array( 'justOne' => false, ); } $conn = $this->connect(); if (empty($conn)) { return false; } try { $bulk = new MongoDB\Driver\BulkWrite(); $bulk->delete($whereArr, $options); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 30000); $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern); return true; } catch (Exception $e) { //记录错误日志 } return false; }
5.执行mand操作:
private function mand($params, $dbName) { $conn = $this->connect(); if (empty($conn)) { return false; } try { $cmd = new MongoDB\Driver\Command($params); $result = $conn->executeCommand($dbName, $cmd); return $result; } catch (Exception $e) { //记录错误 } return false; }
6.统计count:
public function count($query, $collection) { try { $cmd = array( 'count' => $collection, 'query' => $query, ); $res = $this->mand($cmd); $result = $res->toArray(); return $result[0]->n; } catch (Exception $e) { //记录错误 } return false; }
7.聚合distinct:
public function distinct($key, $where, $collection) { try { $cmd = array( 'distinct' => $collection, 'key' => $key, 'query' => $where, ); $res = $this->mand($cmd); $result = $res->toArray(); return $result[0]->values; } catch (Exception $e) { //记录错误 } return false; }
8.aggregate操作
public function aggregate($where, $group, $collection) { try { $cmd = array( 'aggregate' => $collection, 'pipeline' => array( array( '$match' => $where, ), array( '$group' => $group, ), ), 'explain' => false, ); $res = $this->mand($cmd); if (!$res) { return false; } $result = $res->toArray(); return $result[0]->total; } catch (Exception $e) { //记录错误 } return false; }
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对狼蚁SEO的支持。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程