[PHP] new mongodb extension installation and use

Time:2020-3-23

The Mongo extension of the old version is not recommended anymore. The mongodb extension of the new version is generally installed and used above php7

Under Ubuntu

apt-get install php-mongodb

For example, the following code performs query and insert collection operations

mongoManger = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
    }
    //Add document model
    public function addModel($isDraft=false){
        $params=[];
        $params['modelID']='basic_news';
        $params ['name '] =' basic news';
        $params['parentID']='root';
        $params['modelXML']="\r\n\r\n\t  \r\n\t  \r\n\t\t\r\n\t\t\t\r\n\t\t\r\n\t\t\r\n\t\t\t\r\n\t\t\r\n\t\r\n\t\t  \r\n  \r\n\t\r\n\t\t\r\n\t\t\r\n  \r\n";
        $params['isTest']='0';
        $params ['desc '] =' Shihan add ';
        $params['auditFeedback']='';
        $params['status']='1';
       $params['audited']='1';
       $collect=$isDraft ? '.modelDraft':'model';

        $bulk = new MongoDB\Driver\BulkWrite();
        $sets= ['$set' => $params];
        $bulk->update(['modelID' => $params['modelID']],$sets, ['multi' => false, 'upsert' => true]);
        $this->mongoManger->executeBulkWrite($this->dbName.$collect, $bulk);
    }
    //Document model list
    public function listModel($isDraft=false){
        $filter = [];
        $options = [];
        $collect=$isDraft ? '.modelDraft':'model';
        $query = new MongoDB\Driver\Query($filter, $options);
        $cursor = $this->mongoManger->executeQuery($this->dbName.$collect, $query);
        foreach ($cursor as $document) {
            ($document);
        }
    }
    //Get document model details
    public function getModel($isDraft=false){
        $params['modelID']='basic_news';
        $filter = ['modelID'=>$params['modelID']];
        $options = [];
        $collect=$isDraft ? '.modelDraft':'model';
        $query = new MongoDB\Driver\Query($filter, $options);
        $cursor = $this->mongoManger->executeQuery($this->dbName.$collect, $query);
        foreach ($cursor as $document) {
            ($document);
        }
    }
}
$docModel=new DocModel();
$docModel->getModel(true);