本文主要介绍了tp5(thinkphp5)操作mongodb数据库的方法,结合实例形式简单分析了mongodb数据库及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'; } }
相关推荐:
node.js操作mongoDB数据库示例分享_node.js
以上就是tp5操作mongoDB数据库实例的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号