千家信息网

MongoDB操作类PHP代码是怎样的

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,MongoDB操作类PHP代码是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
千家信息网最后更新 2026年11月01日MongoDB操作类PHP代码是怎样的

MongoDB操作类PHP代码是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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

关于MongoDB操作类PHP代码是怎样的问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

字段 条件 数据 数组 格式 文档 之间 数据表 查询 配置 对象 结果 问题 代码 成功 多个 更多 帮助 解答 易行 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网与科技 互联网是第几次科技革命的成果 美国互联网科技巨头监管 腾讯阿里互联网科技 上海亿帐通互联网科技 承德科技产业互联网发展 来此购互联网科技有限公司 彤创互联网科技有限公司 岳阳华臻互联网科技有限公司 宜昌元轩互联网科技有限公司 青海科技产业互联网发展 内蒙古九一互联网科技 互联网科技年终奖 陕西云淼互联网科技公司 半导体行业属于互联网高科技类吗 新氧科技和美国互联网有关系吗 济南小医互联网科技有限公司招聘 互联网科技平台产品运营 马化腾互联网科技教学 知域互联网科技有限公司官网 互联网科技与金融科技 华宇互联网科技 猪兼强互联网科技 互联网高科技企业 互联网时代科技改变生活 电子科技大学互联网创业大赛 洛阳共同富裕互联网科技有限公司 富国互联网金融科技混合基金 西安左小互联网科技有限公司 科技和互联网行业的特点
0