[PHP]新版的mongodb扩展安装和使用
Posted 陶士涵的菜地
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 MongoDBDriverManager("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"?> <model> <fields> <field label="标题" name="title" type="string" widget="title" required="1" maxLength="60" minLength="1" esAnalyzed="analyzed" esAddNoAnalyzed="yes" > <widgetParams> <param name="marks" value="5,13.5,40"/> </widgetParams> <validation> <rule type="maxZhLength" value="40" msgZh="标题长度不能超过40个汉字长度" /> </validation> </field> </fields> <layout> <fieldset name="basic" legend="基本信息"> <field name="title" width="12"/> </fieldset> </layout> </model>"; $params[‘isTest‘]=‘0‘; $params[‘desc‘]=‘shihan添加‘; $params[‘auditFeedback‘]=‘‘; $params[‘status‘]=‘1‘; $params[‘audited‘]=‘1‘; $collect=$isDraft ? ‘.modelDraft‘:‘model‘; $bulk = new MongoDBDriverBulkWrite(); $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 MongoDBDriverQuery($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 MongoDBDriverQuery($filter, $options); $cursor = $this->mongoManger->executeQuery($this->dbName.$collect, $query); foreach ($cursor as $document) { var_dump($document); } } } $docModel=new DocModel(); $docModel->getModel(true);
以上是关于[PHP]新版的mongodb扩展安装和使用的主要内容,如果未能解决你的问题,请参考以下文章