MongoDB操作类PHP代码是怎样的
admin
2023-04-11 20:43:24
0
mongo_hems3;
    }else if($type==2) {
       $mongo_master = $database->mongo162;
    }else if($type == 3) {
         $mongo_master = $database->mongo_yuanchuang;
    } else if($type==5) {
        $mongo_master = $database->mongo_backup;
    }
    else if($type == 0){
        $mongo_master = $database->mongo;
    }
    $host_master = $mongo_master['host'];
    $database    = $mongo_master['database'];
    $port        = $mongo_master['port'];
    $this->debug = true;
//    $this->conn = new Mongo("mongodb://${username}:${password}@${host}");
//    $this->conn = new Mongo("mongodb://${host_master},${host_slave}",array("replicaSet" => "shard"));
    try{
        $this->conn = new Mongo("mongodb://${host_master}:${port}");
    }catch(Exception $e){
        $str = $e."\r\n".date('Y-m-d H:i:s',time())." 连接mongo失败\r\n";
        $this->getMongoLogo($str);
        return false;
    }
//     $this->conn=$db->admin;
//     $this->conn->authenticate('root', '123456');
    $this->db = $this->conn->{$database};
  }
    /**
     * Description 查询配置表hems_basic记录数 可按条件查询记录数
     * @param Array $conditions  可为空  条件格式为数组  array('字段1'=>'字段值1','字段2'=>'字段值2') 表示条件之间是AND关系
     * @return int  $count
     *
     */
    public function getCount($collection,$conditions = array()){
        $this->collection = $this->db->{$collection};
        $count = $this->collection->find($conditions)->count();
        return $count;
    }
    /**
     * Description 根据条件查询配置表hems_basic文档
     * @param  Array $conditions  可为空    条件格式为数组  array('字段1'=>'字段值1','字段2'=>'字段值2') 表示条件之间是AND关系
     * @return Array $res  返回的结果是mongo对象需要遍历才能取出数据
     *
     */
    public function getData($collection,$conditions = array(),$field=array(),$order=array(),$start=0,$pernum=20){
        $this->collection = $this->db->{$collection};
        if(empty($collection)){
            return false;
        }
        try {
            if($start==1&&$pernum==0){
                if($order){
                    $res = $this->collection->find($conditions,$field)->slaveOkay(true)->sort($order);
                }else{
                    $res = $this->collection->find($conditions,$field)->slaveOkay(true);
                }
            }else{
                if($order){
                    $res = $this->collection->find($conditions,$field)->slaveOkay(true)->sort($order)->skip($start)->limit($pernum);
                }else{
                    $res = $this->collection->find($conditions,$field)->slaveOkay(true)->skip($start)->limit($pernum);
                }
            }
        }catch(Exception $e){
            $res = array();
            $str =  date('Y-m-d H:i:s',time())." 从集合".$collection."获取数据\r\n条件为:\r\n";
            $str1= '';
            foreach($conditions as $key => $val){
                $str1.= $key."------------".$val."\r\n";
            }
            $this->getMongoLogo($str.$str1."失败\r\n");
            return false;
        }
        try{
            $res->rewind();
        }catch(Exception $e){
            $str =  date('Y-m-d H:i:s',time())." 从集合".$collection."获取数据\r\n条件为:\r\n";
            $str1= '';
            foreach($conditions as $key => $val){
                $str1.= $key."------------".$val."\r\n";
            }
            $this->getMongoLogo($str.$str1."失败\r\n");
            return false;
        }
        if($res->valid()){
            return iterator_to_array($res,true);
        }else{
            return false;
        }
    }
    /**
     * Description 根据条件查询配置表hems_basic文档
     * @param  Array $conditions  可为空    条件格式为数组  array('字段1'=>'字段值1','字段2'=>'字段值2') 表示条件之间是AND关系
     * @return Array $res  返回的结果是mongo对象需要遍历才能取出数据
     *
     */
    public function getDataOr($collection,$conditions){
        $this->collection = $this->db->{$collection};
        $res = $this->collection->find(array('$or'=>$conditions));
        return $res;
    }
    /**
     * Description 计算数据表中所有文档数(按条件计算,无条件计算整个集合的总数)
     * @param Array $conditions 可为空    条件格式为数组  array('字段1'=>'字段值1','字段2'=>'字段值2') 表示条件之间是AND关系
     * @return int  $count
     */
    public function getIdDataCount($collection,$conditions = array()){
        $this->collection = $this->db->{$collection};
        $count = $this->collection->find($conditions)->count();
        return $count;
    }
    /**
     * Description 根据条件或者配置表中的_id到hems_data数据表中取出对应所有文档文档
     * @param Array $conditions 可为空   条件格式为数组  array('字段1'=>'字段值1','字段2'=>'字段值2') 表示条件之间是AND关系
     * @return Array $res  返回的结果是mongo对象需要遍历才能取出数据
     */
    public function getIdData($collection,$conditions){
        $this->collection = $this->db->{$collection};
        $res = $this->collection->find($conditions);
        return $res;
    }
    /**
     * Description 根据条件修改数据表文档
     * @param Array $conditions $data   $conditions格式为数组  array('字段1'=>'字段值1','字段2'=>'字段值2') 表示多个条件并列
     *                                  $data格式为数组  array('字段1'=>'字段值1','字段2'=>'字段值2') 表示修改的字段
     */
    public function editData($collection,$conditions,$data){
        $this->collection = $this->db->{$collection};
        if($this->collection->update($conditions ,array('$set'=>$data),array('multiple'=>1))){
            $code = 1;
            $msg  = '修改成功!';
        }else{
            $code = 0;
            $msg  = '修改失败!';
        }
        return array('code'=>$code,'msg'=>$msg);
    }
    
    /*
     * Description 根据条件删除数据表文档
     * @param Array $conditions    $conditions格式为数组  array('字段1'=>'字段值1','字段2'=>'字段值2') 表示多个条件并列
     */
    public function delData($collection,$conditions){
        $this->collection = $this->db->{$collection};
        if($this->collection->remove($conditions)){
            $code = 1;
            $msg  = '删除成功!';
        }else{
            $code = 0;
            $msg  = '删除失败!';
        }
        return array('code'=>$code,'msg'=>$msg);
   }
    /*
     * Description 插入操作
     * 根据主键_id判断,如果该记录已经存在则
     */
    public function save($collection,$field,$flag=0){
        $this->collection = $this->db->{$collection};
        $log = "";
        foreach($field as $kdata => $vdata){
            $log .= $kdata.":".$vdata.",";
        }
        $log = trim($log,",");
        $newLog = "\r\n-------\r\ntime:".date("Y-m-d H:i:s")."\r\ndata".$log;
        $oldLog = file_get_contents("../tmp/mongoLog.txt");
        @file_put_contents("../tmp/mongoLog.txt",$oldLog.$newLog);
        if($flag){
          $result = $this->collection->save($field,array('safe'=>1));
        }else{
          $result = $this->collection->insert($field,array('safe'=>1));
        }
        return $result;
    }
   
}
?>

相关内容

热门资讯

徐巧芯剖析赖清德:最怕自己丢掉... 海峡导报综合报道 大罢免投票一周年纪念活动26日举行,台北市长蒋万安出席并怒轰,民进党一年过后仍没有...
中国红十字会总会紧急向广东调拨... 记者从中国红十字会总会了解到,针对台风“红霞”给广东造成的灾情,中国红十字会总会启动四级应急响应,紧...
这些少年,正被“毒蛋”围猎 李梦抽了几口电子烟,吐出了白色的烟雾,有一股浓烈的水果香味。随后,她看着自己握电子烟的手,跟身边人说...
蒋万安号召“倒阁”,卢秀燕认为... 海峡导报综合报道 台北市长蒋万安27日抛出震撼议题,表示多名民代向他提议推动“倒阁”,要求台行政机构...
特朗普将在白宫会见两位重要客人 俄乌战争仍未结束,美伊冲突又有升级风险。在此背景下,美国总统特朗普将在白宫接见乌克兰和以色列领导人。...
新租客打扫卧室柜子上掉落4万元... 近日,莲前西路某小区内,张倩签下一套房子的租赁合同。当天上午,她带着12岁的大女儿周馨怡和11岁的小...
六部门通告:禁止涉军队退役报废... 关于禁止涉军队退役报废装备销售活动的通告近年来,部分经营者在线上线下公开销售涉及我军退役报废装备(指...
九寨沟景区发生泥石流,部分道路... 7月26日,九寨沟风景名胜区管理局发布泥石流灾害通报:2026年7月26日15时50分,九寨沟景区局...
空调26度不凉是什么原因 从空调内部元器件来说,不凉的原因有可能是温度传感器出现故障了,也有可能是空调压缩机或者铜管出现问题,...
鲸鸿动能携手香港国际机场、鸿蒙...   2026 年 7 月 25 日,十余位鸿蒙车主组成的车队从珠海出发,经港珠澳大桥通关抵达香港国际...