如何在PHP7中使用mongoDB扩展-创新互联
这篇文章将为大家详细讲解有关如何在PHP7中使用 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; }
关于如何在PHP7中使用 mongoDB扩展就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网页标题:如何在PHP7中使用mongoDB扩展-创新互联
URL地址:http://ybzwz.com/article/pghdd.html