前言
最近在做的项目需要将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 (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.执行command操作:
private function command($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->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; }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“PHP7 mongoDB扩展使用的方法分享”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?