前言
最近在做的项目需要将PHP5.6升级到PHP7.0,使用过PHP-mongo扩展的同学应该知道,PHP7.0的mongodb扩展是完全不兼容PHP5.6的mongo扩展的,php-mongodb改如何使用呢。
下面直接说明各种方法的使用:
1.mongodb连接:
代码语言:javascript复制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 MongoDBDriverManager($connStr, $options);
return $mc;
}
catch(Exception $e){
return false;
}
}
2.查询find:
代码语言:javascript复制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 MongoDBDriverQuery($query, $options);
$readPreference = new MongoDBDriverReadPreference(MongoDBDriverReadPreference::RP_SECONDARY);
$cursor = $conn- executeQuery($collection, $mongoQuery, $readPreference);
foreach($cursor as $value) {
$data[] = (array)$value;
}
return $data;
} catch (Exception $e) {
//记录错误日志
}
return false;
}
3.插入操作insert:
代码语言:javascript复制public function insert($addArr, $collection) {
if (empty($addArr) || !is_array($addArr)) {
return false;
}
$conn = $this- connect();
if (empty($conn)) {
return false;
}
try {
$bulk = new MongoDBDriverBulkWrite();
$bulk- insert($addArr);
$writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 6000);
$result = $conn- executeBulkWrite($collection, $bulk, $writeConcern);
if ($result- getInsertedCount()) {
return true;
}
} catch (Exception $e) {
//记录错误日志
}
return false;
}
4.删除delete:
代码语言:javascript复制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 MongoDBDriverBulkWrite();
$bulk- delete($whereArr, $options);
$writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 30000);
$result = $conn- executeBulkWrite($collection, $bulk, $writeConcern);
return true;
} catch (Exception $e) {
//记录错误日志
}
return false;
}
5.执行command操作:
代码语言:javascript复制private function command($params, $dbName) {
$conn = $this- connect();
if (empty($conn)) {
return false;
}
try {
$cmd = new MongoDBDriverCommand($params);
$result = $conn- executeCommand($dbName, $cmd);
return $result;
} catch (Exception $e) {
//记录错误
}
return false;
}
6.统计count:
代码语言:javascript复制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:
代码语言:javascript复制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操作:
代码语言:javascript复制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;
}