前言
最近在做的項(xiàng)目需要將PHP5.6升級(jí)到PHP7.0,使用過(guò)PHP-mongo擴(kuò)展的同學(xué)應(yīng)該知道,PHP7.0的mongodb擴(kuò)展是完全不兼容PHP5.6的mongo擴(kuò)展的,php-mongodb改如何使用呢。
下面直接說(shuō)明各種方法的使用:
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) {
//記錄錯(cuò)誤日志
}
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) {
//記錄錯(cuò)誤日志
}
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) {
//記錄錯(cuò)誤日志
}
return false;
}
5.執(zhí)行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) {
//記錄錯(cuò)誤
}
return false;
}
6.統(tǒng)計(jì)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) {
//記錄錯(cuò)誤
}
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) {
//記錄錯(cuò)誤
}
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) {
//記錄錯(cuò)誤
}
return false;
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
您可能感興趣的文章:- PHP擴(kuò)展Swoole實(shí)現(xiàn)實(shí)時(shí)異步任務(wù)隊(duì)列示例
- PHP基于mcript擴(kuò)展實(shí)現(xiàn)對(duì)稱加密功能示例
- PHP安裝memcache擴(kuò)展的步驟講解
- PHP安裝BCMath擴(kuò)展的方法
- PHP擴(kuò)展mcrypt實(shí)現(xiàn)的AES加密功能示例
- php的擴(kuò)展寫法總結(jié)