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;
    }
   
}
?>

相关内容

热门资讯

辟谣!河南公布7起网络谣言典型... 4月,河南持续做好涉企业、“三农”、公共政策、社会热点、文化旅游网络谣言的发现辟除,有力打击谣言传播...
企业买AI,从“尝鲜”变成“算... 【大河财立方 记者 陈薇】以前企业采购人工智能大模型,大多只是抱着试一试的心态:这笔费用归在创新预算...
宇树发布全球首款量产版载人机甲... 【大河财立方消息】 5月12日,据宇树科技官微,宇树发布GD01载人变形机甲,定价390万元起。据介...
中国驻荷兰使馆提醒:在荷中国公... 根据世界卫生组织近日通报,一艘极地探险邮轮上出现了汉坦病毒感染病例。截至5月11日,造成至少8例感染...
新乡市城管局公布典型案例:非法... 近日,新乡市城市管理局对外公布一起危害城市水环境安全的污染环境典型案例。该案经多部门联合溯源、侦办、...
敲诈900多家网店 买家恶意下... 先是申请“仅退款”企图免费得到商品,被商家拒绝后就投诉店铺、恶意下单、索要“和解费”,网店老板曹先生...
公司购买50箱“飞天茅台”,竟... 邢台一公司购买50箱飞天茅台酒,经鉴定竟有41箱为假冒茅台酒,仅9箱为真茅台酒。经查,涉案男子龚某从...
穿越周期 不止“万亿” ——从... 洛阳钼业巴西金矿一角。市民在郑州金融岛参加由好想你公司主办的“好好生活”户外活动。光力科技半导体晶圆...
魏平政出线几率高!国民党整合拼... 海峡导报综合报道 国民党迟未公布彰化县长人选,地方盛传,周三国民党中常可能会有答案,引发关注。对此,...
人均奖金600万,知道内存为什... 唯物的中国芯片产业深度观察AI带动存储狂飙,韩国存储大厂SK海力士也跟着起飞。近日,有投行预测其年终...