这次给大家带来tp5(thinkPHP5)操作mongoDB数据库步骤详解,tp5(thinkPHP5)操作mongoDB数据库的注意事项有哪些,下面就是实战案例,一起来看一下。
1.通过composer安装
composer require mongodb/mongodb
2.使用
<?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'; } }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上是tp5(thinkPHP5)操作mongoDB数据库步骤详解的详细内容。更多信息请关注PHP中文网其他相关文章!