PHP7 mongoDB扩展使用的方法分享
发布:smiling 来源: PHP粉丝网 添加日期:2021-11-20 20:07:05 浏览: 评论:0
这篇文章主要给大家介绍了关于PHP7 mongoDB扩展使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用PHP7具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
前言
最近在做的项目需要将PHP5.6升级到PHP7.0,使用过PHP-mongo扩展的同学应该知道,PHP7.0的mongodb扩展是完全不兼容PHP5.6的mongo扩展的,php-mongodb改如何使用呢。
下面直接说明各种方法的使用:
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 (emptyempty($conn)) {
- return false;
- }
- try {
- $data = array();
- $options = array();
- if (!emptyempty($query)) {
- $options['projection'] = array_fill_keys($fields, 1);
- }
- if (!emptyempty($sort)) {
- $options['sort'] = $sort;
- }
- if (!emptyempty($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 (emptyempty($addArr) || !is_array($addArr)) {
- return false;
- }
- $conn = $this->connect();
- if (emptyempty($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 (emptyempty($whereArr)) {
- return false;
- }
- if (!isset($options['justOne'])) {
- $options = array(
- 'justOne' => false,
- );
- }
- $conn = $this->connect();
- if (emptyempty($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.执行command操作:
- private function command($params, $dbName) {
- $conn = $this->connect();
- if (emptyempty($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->command($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->command($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->command($cmd);
- if (!$res) {
- return false;
- }
- $result = $res->toArray();
- return $result[0]->total;
- } catch (Exception $e) {
- //记录错误
- }
- return false;
- }
Tags: mongoDB PHP7扩展使用
- 上一篇:详解提高使用Java反射的效率方法
- 下一篇:PHP利用百度ai实现文本和图片审核
相关文章
- ·使用php mongodb扩展时比较需要注意的事项(2014-03-22)
- ·PHP实现GRIDFS存储文件到MONGODB的教程(2015-12-10)
- ·MONGODB GRIDFS存取文件PHP示例(2015-12-10)
- ·在MongoDB上如何进行数据的导入与导出(2020-03-22)
- ·MongoDB在PHP中的常用操作小结(2020-09-17)
- ·php中的mongodb select常用操作代码示例(2021-04-10)
- ·php实现Mongodb自定义方式生成自增ID的方法(2021-05-17)
- ·PHP实现的mongoDB数据库操作类完整实例(2021-09-08)
- ·PHP使用mongoclient简单操作mongodb数据库示例(2021-11-06)
- ·mongodb和php的用法详解(2021-11-14)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)