多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
首先,确保你已经安装了MongoDB driver for PHP(重要),参考: ~~~ http://pecl.php.net/package/mongodb ~~~ 然后使用Composer安装ThinkPHP5.0的MongoDb驱动: ~~~ composer require topthink/think-mongo=1.* ~~~ 执行结果: ~~~ D:\phpStudy\PHPTutorial\WWW\workspace\report>composer require topthink/think-mongo=1.* ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing topthink/think-mongo (v1.8.4): Downloading (100%) Package activecollab/etcd is abandoned, you should avoid using it. No replacemen t was suggested. Writing lock file Generating autoload files ~~~ 5.0版本的核心框架支持think-mongo扩展的版本是1.* 版本 修改你的数据库配置文件database.php中的type参数为: ~~~ 'type' => '\think\mongo\Connection', ~~~ 接下来可以使用Db类直接操作MongoDb了,例如: ~~~ Db::name('demo') ->find(); Db::name('demo') ->field('id,name') ->limit(10) ->order('id','desc') ->select(); ~~~ 或者使用模型操作: ~~~ User::get(1); User::all('1,2,3'); ~~~ MongoDb默认的主键是_id并且是一个ObjectID对象,如果需要和mysql一样使用id作为主键,可以如下参数: ~~~ // 强制把_id转换为id 'pk_convert_id' => true, ~~~ * * * * * https://www.kancloud.cn/manual/thinkphp5/167865