温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在PHP7中使用 mongoDB扩展

发布时间:2021-05-17 16:57:48 来源:亿速云 阅读:147 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关如何在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扩展就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI