123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/5/8 0008
- * Time: 14:38
- */
- class IspMongo
- {
- static private $mongo_obj = null;
- /**
- * 查询mongo
- * @param $table_name
- * @param array $params
- * @param array $other_pra
- * @return array
- */
- static public function selectMongo($table_name,$params = array(),$other_pra = array()){
- $res = array();
- $mongo = Yii::app()->params['mongodb'];
- $mongo_server = $mongo['server'];
- $mongo_db = $mongo['db'];
- try {
- // if(class_exists('MongoClient')){
- // self::$mongo_obj = new MongoClient($mongo_server);
- // }else{
- // }
- self::$mongo_obj = new Mongo($mongo_server);
- $collection=self::$mongo_obj->selectCollection($mongo_db,$table_name);
- if (isset($other_pra['fields']) && $other_pra['fields']) {
- $fields = $other_pra['fields'];
- }else{
- $fields = array();
- }
- if (isset($other_pra['sort']) && $other_pra['sort']) {
- $sort = $other_pra['sort'];
- }else{
- $sort = array();
- }
- if (isset($other_pra['limit']) && $other_pra['limit']) {
- $limit = $other_pra['limit'];
- }else{
- $limit = array();
- }
- //返回数据是否重建索引
- if (isset($other_pra['is_index'])) {
- $is_index = $other_pra['is_index'];
- }else{
- $is_index = true;
- }
- $cursor = $collection->find($params,$fields);
- if($sort){
- $cursor->sort($sort);
- }
- if($limit){
- $cursor->limit($limit);
- }
- if($cursor){
- $res = iterator_to_array($cursor);
- }
- self::close();
- if($res && is_array($res)){
- if ($is_index) {
- $_res = $res;
- $res = array();
- foreach ($_res as $re) {
- $res[$re['_id']] = $re;
- }
- }
- unset($_res);
- }
- } catch (Exception $ex) {
- exit($ex->getMessage());
- }
- return $res;
- }
- static public function close()
- {
- if (self::$mongo_obj) {
- self::$mongo_obj->close();
- }
- }
- }
|