php MongoDB database connection, deletions change search the database

A connection database (the complete extension of the premise MongoDB)

mongodb local connection, the default port 27017,

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

Second, insert data

? < PHP 
    
    // connection Mongodb 
    $ Manager = new new MongoDB \ Driver \ Manager ( "MongoDB: //127.0.0.1: 27017" ); 
    
    echo "Connect Success !!!" ; 
    
    // create a variable, insert operations 
    $ Bulk = new new MongoDB \ Driver \ BulkWrite; 
    
    // data insertion (insertion of one or more) 
    $ Bulk -> iNSERT ([ "name" => "zhangsan", "Age" => 25, "class" => "jisuanjierban" ]);
     $ Bulk -> iNSERT ([ "name" => "Lisi", "Age" => 25, "class" => "jisuanjisiban" ]); 
    
    // inserting data into the database useinfo student table 
    $ manager ->executeBulkWrite("useinfo.student",$bulk);
    
    echo "insert success!!!";
?>

Third, the query data

? < PHP 
 
    // connection Mongodb 
    $ Manager = new new MongoDB \ Driver \ Manager ( "MongoDB: //127.0.0.1: 27017" ); 
    
    echo "Connect Success !!!." PHP_EOL ; 
    
    // filter condition 
    $ filter   = [ ' Age '=> [' $ gt '=> 20 is ]]; 
    
    // options 
    $ option = [
        
         ' Projection '=> [ "the _id" => 0],   // do not show _id, this field is used to specify the display or certain fields
         
        'Sort' => [ 'name' => -1],      // Sort according to some fields, -1 flashback, a positive-sequence
         
        'limit' => $ the pageSize ,   // how many data taking
         
        'skip'=> ($num-1)*$pageSize  // take the first few pages of data 
    ]; 
    
    // query data 
    $ Query = new new MongoDB \ Driver \ Query ( $ filter , $ Option ); 
    
    // query results returned array 
    $ Cursor = $ Manager -> the executeQuery ( 'useinfo.student ', $ Query ); 
    
    // loop through 
    the foreach ( $ Cursor  AS  $ Document ) { 
    
        print_r ( $ Document ); 
    } 
    
    echo "over" ;
    
 ?>

Fourth, update data

<?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, all records represent updates found, false default update found first//
    
    
    );
                                                // If there is no representative of the upsert is true this record to insert, the insert is not false by default
    
     $ Result = $ Manager -> executeBulkWrite ( 'test.sites', $ Bulk );
 >?

Fifth, the above simple method of packaging

? < PHP 
     class MongoDBClient { 
        
        protected  $ Manager ;   // connected database 
        
        protected  $ dbname ;    // database operations 
        
        protected  $ Collection ;   // table in the database operations 
        
        protected  $ Bulk ;         // insert and update operations 
        
        protected  $ writeConcern ;   / / connection timeout 
        
        
        // constructor defined 
        public  function the __construct ( $ config ) {
             $ the this -> = Manager new new MongoDB \ Driver \ Manager ( "MongoDB: //127.0.0.1: 27017");   //Connecting to the database 
             
            $ the this -> dbname = $ config [ "dbname"];    // initialize the database 
            
            $ the this -> Collection = $ config [ "Collection"];   // initialize set 
            
            $ the this -> Bulk = $ new new MongoDB \ Driver \ BulkWrite;   // insert, update, manipulate 
        
            $ the this -> writeConcern = new new MongoDB \ Driver \ WriteConcern (MongoDB \ Driver \ WriteConcern :: MAJORITY, 100); // connection timeout 

        } 
        
        // query method 
        public  function query ( $ filter = [ ], $ the Option = []) {
             // create an object query 
            $ query= New new MongoDB \ Driver \ Query ( $ filter , $ the Option ) 
            
            // query results, returns an array of 
            $ the Cursor = $ Manager -> executeQuery ( " $ the this -> dbname. $ The this , -> Collection" $ uery ); 
            
            // loops through 
            the foreach ( $ Cursor  AS  $ Document ) { 
            
                print_r ( $ Document ); 
            } 
        
        } 
        
        // insert data 
        public  function iNSERT ( $ data = []) { 
            
            //Inserting one or more data bulk- $> INSERT ([ "name" => "zhangsan", "Age" => 25, "class" => "jisuanjierban"]); 
            $ the this -> bulk-> INSERT ( $ data ); 
            
            // table data is inserted into the designated database 
            $ the this -> Manager-> the executeQuery ( " $ the this . -> dbname $ the this -> Collection", $ the this -> Bulk); 
            
        
        } 
        
        // update data 
        public  function Update ( $ filter = [], $ Update = [], $ the upsert = to false ) { 
            
            $ the 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 $ = $ the this -> Manager-> exeucuteBulkWrite ( " $ the this -> dbname. $ the this -colletion", $ the this - Bulk); 
        
        } 
        
        
    } 
    
    // Get database to operate 
    $ dbname = $ _GET [ 'dbname' ] ; 
    
    // table to be operated (set) 
    $ collection = $ _GET [ 'colection' ]; 
    
    // Create the current class object 
    $ OBJ1 = new new MongoDBClient ([ "dbname" => $ dbname , "collection" => $ collection ]); 
    
    // get the current operation type (add, delete, change, search) from the form 
    $ 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);
    
    Update Data//
    }
    else if($action == "update"){
        $filter = $_GET["filter"];
        $update = $_GET["date"];
        $obj1->update($filter,$update);
    }
    
    //删除数据
    else if($action == "delete"){
        $filter = $_GET("filter");
        $obj1->delete($filter);
    }
?>

 

Guess you like

Origin www.cnblogs.com/liaopeng123/p/11511585.html