日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

php7.0的mongodb擴(kuò)展是完全不兼容以前老的php5.6的mongo擴(kuò)展的,在這里對mongodb的擴(kuò)展做一個簡單的總結(jié)。

廢話不多說直接寫代碼

# 創(chuàng)建mongodb鏈接
public function connect($conf_arr) {
 try{
 $conn_str = "mongodb://" . $conf_arr['host'] . ":" . $conf_arr['port'] . "/" . $conf_arr['db_name'];
 $options = array(
 'username' => $conf_arr['username'],
 'password' => $conf_arr['password'],
 'readPreference' => $conf_arr['read_preference'],
 'connectTimeoutMS' => intval($conf_arr['connect_timeout_ms']),
 'socketTimeoutMS' => intval($conf_arr['socket_timeout_ms']),
 );
 return new MongoDBDriverManager($conn_str, $options);
 }
 catch(Exception $e){
 return false;
 }
}
# find查詢方法
public function find($query = [], $fields = [], $collection, $sort = [], $limit = 0, $skip = 0) {
 $conn = $this->connect();
 if (empty($conn)) return false;
 try {
 $data = [];
 $options = [];
 if (!empty($query)) {
 $options['projection'] = array_fill_keys($fields, 1);
 }
 if (!empty($sort)) $options['sort'] = $sort;
 if (!empty($limit)) $options['limit'] = $limit;
 if (!empty($skip)) $options['skip'] = $skip;
 $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) {
 // write log
 }
 return false;
}
# insert 插入方法
public function insert($add_arr, $collection) {
 if (empty($add_arr) || !is_array($add_arr) || '' == $collection) {
 return false;
 }
 $conn = $this->connect();
 if (empty($conn)) {
 return false;
 }
 try {
 $bulk = new MongoDBDriverBulkWrite();
 $bulk->insert($add_arr);
 $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 6000);
 $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern);
 if ($result->getInsertedCount()) {
 return true;
 }
 } catch (Exception $e) {
 // write log pass
 }
 return false;
}
# delete 刪除方法
public function delete($where_arr, $options = [], $collection) {
 if (empty($where_arr) || '' == $collection) {
 return false;
 }
 if (!isset($options['justOne'])) {
 $options['justOne'] = false;
 }
 $conn = $this->connect();
 if (empty($conn)) {
 return false;
 }
 try {
 $bulk = new MongoDBDriverBulkWrite();
 $bulk->delete($where_arr, $options);
 $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 30000);
 $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern);
 return true;
 } catch (Exception $e) {
 // write log pass
 }
 return false;
}
# 執(zhí)行cmd命令操作
public 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) {
 // write log pass
 }
 return false;
}
# distinct方法
public function distinct($key, $where, $collection) {
 try {
 $cmd = [
 'distinct' => $collection,
 'key' => $key,
 'query' => $where,
 ];
 $res = $this->command($cmd);
 $result = $res->toArray();
 return $result[0]->values;
 } catch (Exception $e) {
 // write log pass
 }
 return false;
}
# count方法
public function count($query, $collection) {
 try {
 $cmd = [
 'count' => $collection,
 'query' => $query,
 ];
 $res = $this->command($cmd);
 $result = $res->toArray();
 return $result[0]->n;
 } catch (Exception $e) {
 // write log pass
 }
 return false;
}
# aggregate 方法
public function aggregate($where, $group, $collection) {
 try {
 $cmd = [
 'aggregate' => $collection,
 'pipeline' => [
 ['$match' => $where],
 ['$group' => $group],
 ],
 'explain' => false,
 ];
 $res = $this->command($cmd);
 if (!$res) {
 return false;
 }
 $result = $res->toArray();
 return $result[0]->total;
 } catch (Exception $e) {
 // write log
 }
 return false;
}
php7的Mongodb擴(kuò)展筆記

 

分享到:
標(biāo)簽:php7 Mongodb
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達(dá)人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定