[PHP]新版的mongodb扩展安装和使用

旧版的mongo扩展已经不推荐使用了,在php7以上一般是安装和使用新版的mongodb扩展

ubuntu下

apt-get install php-mongodb

例如下面的代码进行了查询和插入集合操作

<?php
class DocModel{
    public $mongoManger=null;
    public $dbName='coms';
    public function __construct(){
        // 连接到mongodb
        $this->mongoManger = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
    }
    //添加文档模型
    public function addModel($isDraft=false){
        $params=[];
        $params['modelID']='basic_news';
        $params['name']='基础新闻';
        $params['parentID']='root';
        $params['modelXML']="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<model>\r\n\t  <fields>\r\n\t  <field label=\"标题\" name=\"title\" type=\"string\" widget=\"title\" required=\"1\" maxLength=\"60\" minLength=\"1\"   esAnalyzed=\"analyzed\" esAddNoAnalyzed=\"yes\" >\r\n\t\t<widgetParams>\r\n\t\t\t<param name=\"marks\" value=\"5,13.5,40\"\/>\r\n\t\t<\/widgetParams>\r\n\t\t<validation>\r\n\t\t\t<rule type=\"maxZhLength\" value=\"40\" msgZh=\"标题长度不能超过40个汉字长度\" \/>\r\n\t\t<\/validation>\r\n\t<\/field>\r\n\t\t  <\/fields>\r\n  <layout>\r\n\t<fieldset name=\"basic\" legend=\"基本信息\">\r\n\t\t<field name=\"title\" width=\"12\"\/>\r\n\t<\/fieldset>\t\r\n  <\/layout>\r\n<\/model>";
        $params['isTest']='0';
        $params['desc']='shihan添加';
        $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);
    }
    //文档模型列表
    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) {
            var_dump($document);
        }
    }
    //获取文档模型详情
    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) {
            var_dump($document);
        }
    }
}
$docModel=new DocModel();
$docModel->getModel(true);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值