前言
最近在做的项目需要将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( 'urname' => $confarr['urname'], '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 fal; }}
2.查询find:
public function find($query = array(), $fields = array(), $collection, $sort = array(), $limit = 0, $skip = 0) { $conn = $this->connect(); if (empty($conn)) { return fal; } 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_condary); $cursor = $conn->executequery($collection, $m山大威海分校ongoquery, $readpreference); foreach($cursor as $value) { $data[] = (array)$value; } return $data; } catch (exception $e) { //记录错误日志 } return fal;}
3.插入操作inrt:
public function inrt($addarr, $collection) { if (empty($addarr) || !is_array($addarr)) { return fal; } $conn = $this->connect(); if (empty($conn)) { return fal; } try { $bulk = new mongodb\driver\bulkwrite(); $bulk->inrt($addarr); $writeconcern = new mongodb\driver\writeconcern(mongodb\driver\writeconcern::majority, 6000); $result = $conn->executebulkwrite($collection, $bulk, $writeconcern); if ($result->getinrtedcount()) { return true; } } catch (exception $e) { //记录错误日志 } return fal;}
4.删除delete:
public function delete($wherearr, $options = array(), $collection) { if (empty($wherearr)) { return fal; } if (!ist($options['justone'])) { $options = array( 'justone' => fal, ); } $conn = $this->connect(); if (empty($conn)) { return fal; } 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 fal;}
5.执行command操作:
private function command($params, $dbname) { $conn = $this->connect(); if (empty($conn)) { return fal; } try { $cmd = new mongodb\driver\command($params); $result = $conn->executecommand($dbname, $cmd); return $result; } catch (exception $e) { //记录错误 } return fal;}
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 fal;}
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 fal;}
8.aggregate操作:
public function aggregate($where, $group, $collection) { try { $cmd = array( 'aggregate' => $collection, 'pipeline' => array( array( '$match' => $where, ), array( '$group' => $group, ), ), 'explain' => fal, ); $res = $this->command($cmd); if (!$res作文我和神笔马良的一天) { return fal; } $result = $res->toarray(); return $result[0]->total; } catch (exception $e) { //记录错误 } return fal;}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对www.887551.com的支持。
本文发布于:2023-04-07 08:55:57,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/a76b6b9cc5c0a18960d58a3da96695ce.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP7 mongoDB扩展使用的方法分享.doc
本文 PDF 下载地址:PHP7 mongoDB扩展使用的方法分享.pdf
留言与评论(共有 0 条评论) |