ThinkPHP5集成MongoDB概述
本示例详细说明了如何在thinkPHP5框架环境下对接并操作mongoDB数据库,供广大开发者参考借鉴。
第一步:执行composer安装命令。
composer require mongodb/mongodb

第二步:进行代码层面的具体使用。
<?php
/**
* @author: jim
* @date: 2017/11/17
*/
namespace app\index\controller;
use think\Controller;
use MongoDB\Driver\Manager;
use MongoDB\Collection;
class MongoTest extends Controller
{
protected $mongoManager;
protected $mongoCollection;
public function __construct()
{
$this->mongoManager = new Manager($this->getUri());
$this->mongoCollection = new Collection($this->mongoManager, "mldn","dept");
}
public function test() {
// 读取一条数据
$data = $this->mongoCollection->findOne();
print_r($data);
}
protected function getUri()
{
return getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1:27017';
}
}

期望上述内容能为基于ThinkPHP框架的PHP程序设计工作提供有效帮助。