• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php连接MongoDB数据库,对数据库的增删改查

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、连接数据库(在完成对MongoDB的扩展的前提下)

连接本地的mongodb,端口默认27017,

<?php
    
    //连接Mongodb
    $manager = new      MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
    
    echo "connect success!!!";    
?>

二、插入数据

<?php
    
    //连接Mongodb
    $manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
    
    echo "connect success!!!";
    
    //创建变量,进行插入操作
    $bulk = new MongoDB\Driver\BulkWrite;
    
    //插入的数据(插入一条或多条)
    $bulk->insert(["name"=>"zhangsan","age"=>25,"class"=>"jisuanjierban"]);
    $bulk->insert(["name"=>"lisi","age"=>25,"class"=>"jisuanjisiban"]);
    
    //插入数据到useinfo数据库下的student表
    $manager->executeBulkWrite("useinfo.student",$bulk);
    
    echo "insert success!!!";
?>

三、查询数据

<?php
 
    //连接Mongodb
    $manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
    
    echo "connect success!!!".PHP_EOL;
    
    //过滤条件
    $filter  = ['age' => ['$gt' => 20]];
    
    //可选项
    $option = [
        
        'projection' => ["_id" =>0],  //不显示_id,此字段用于指定显示或不显示某些字段
        
        'sort' => ['name' => -1],     //按照某些字段排序 ,-1倒叙,1正序
        
        'limit'=>$pageSize,  //取多少条数据
        
        'skip' => ($num-1)*$pageSize  //取第几页数据
    ];
    
    //查询数据
    $query = new MongoDB\Driver\Query($filter, $option);
    
    //查询结果,返回数组
    $cursor = $manager->executeQuery('useinfo.student',$query);
    
    //循环遍历
    foreach ($cursor as $document){
    
        print_r($document);
    }
    
    echo "over";
    
?>

四、更新数据

<?php 
    //连接Mongodb
    $manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
    
    echo "connect success!!!"
    
    $bulk->new MongoDB\Driver\BulkWrite;
    $bulk->update(
        ["name" => "zhangsan"],
        ["$set" => ["name" => "kenan","class" => "softeneregy"]],
        ["multi" => false, "upsert" => false],  //multi为true,代表更新找到的所有记录,false默认更新找到的第一条
                                                //upsert为true代表如果没有此条记录就执行插入操作,默认false不插入
    );
    
    $result = $manager->executeBulkWrite('test.sites', $bulk);
?>

五、对以上方法进行简单的封装

<?php 
    class MongoDBClient{
        
        protected $manager;  //连接数据库
        
        protected $dbname;   //操作的数据库
        
        protected $collection;  //操作的数据库下的表
        
        protected $bulk;        // 插入和更新操作
        
        protected $writeConcern;  //连接超时
        
        
        //定义构造函数
        public function __construct($config){
            $this->manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");  //连接数据库
             
            $this->dbname = $config["dbname"];   //初始化数据库
            
            $this->collection = $config["collection"];  //初始化集合
            
            $this->bulk = $new MongoDB\Driver\BulkWrite;  //插入、更新操纵
        
            $this->writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY,100); //连接超时

        }
        
        //查询方法
        public function query($filter=[],$option=[]){
            //创建查询对象
            $query = new MongoDB\Driver\Query($filter,$option)
            
            //查询结果,返回数组
            $cursor = $manager->executeQuery("$this->dbname.$this->collection",$uery);
            
            //循环遍历
            foreach($cursor as $document){
            
                print_r($document);
            }
        
        }
        
        //插入数据
        public function insert($data=[]){
            
            //插入一条或多条数据 $bulk->insert(["name"=>"zhangsan","age"=>25,"class"=>"jisuanjierban"]);
            $this->bulk->insert($data);
            
            //插入数据到指定的数据库下的表
            $this->manager->executeQuery("$this->dbname.$this->collection",$this->bulk);
            
        
        }
        
        //更新数据
        public function update($filter=[],$update=[],$upsert=false){
            
            $this->bulk->update([
                $filter,
                ["$set" => $update],
                ["multi" => true,"upsert"=>$upsert],
            ]);
            $result = $this->manager-executeBulkWrite("$this->dbname.$this->collection",$this-bulk);
            
            
        }
        
        //删除数据
        public function delete($filter=[],$limit=0){
        
            $this-bulk-delete(
                $filter,
                ['limit' => $limit]
            );
            $result = $this->manager->exeucuteBulkWrite("$this->dbname.$this-colletion",$this-bulk);
        
        }
        
        
    }
    
    //获取要操作的数据库
    $dbname = $_GET['dbname'];
    
    //要操作的表(集合)
    $collection = $_GET['colection'];
    
    //创建当前类的对象
    $obj1 = new MongoDBClient(["dbname"=>$dbname,"collection"=>$collection]);
    
    //从表单获取当前操作类型(增、删、改、查)
    $action = $_GET['action'];
    
    //插入数据
    if($action == "insert"){
        $data = $_GET["data"];
        $obj1->insert($data);
        
    //查询数据
    }else if($action == "query"){
        $filter = $_GET["filter"];
        $option = $_GET["option"];
        $obj->query($filter,$option);
    }
    
    //更新数据
    else if($action == "update"){
        $filter = $_GET["filter"];
        $update = $_GET["date"];
        $obj1->update($filter,$update);
    }
    
    //删除数据
    else if($action == "delete"){
        $filter = $_GET("filter");
        $obj1->delete($filter);
    }
?>

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用PHP、MySQL实现修改密码+防止通过url强行进入系统发布时间:2022-07-10
下一篇:
PHP读写指定URL参数的方法发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap