Method sharing used by PHP7 mongoDB extension

  • 2021-12-09 08:39:44
  • OfStack

Preface

The recent project needs to upgrade PHP5.6 to PHP7.0. Students who have used PHP-mongo extension should know that mongodb extension of PHP7.0 is completely incompatible with mongo extension of PHP5.6. How to use php-mongodb instead.

The use of various methods is directly explained below:

1. mongodb connection:


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. Query 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) {
  // Log errors 
 }
 return false;
}

3. Insert 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) {
  // Log errors 
 }
 return false;
}

4. Delete 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) {
  // Log errors 
 }
 return false;
}

5. Perform the command operation:


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) {
  // Logging error 
 }
 return false;
}

6. Statistical 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) {
  // Logging error 
 }
 return false;
}

7. Aggregate 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) {
  // Logging error 
 }
 return false;
}

8. aggregate operation:


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) {
  // Logging error 
 }
 return false;
}

Summarize


Related articles: