欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-07 12:44:51
...

一、连接数据库(在完成对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);
    }
?>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关标签: mongodb