tp5(thinkPHP5)操作mongoDB数据库的方法

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了tp5(thinkPHP5)操作mongoDB数据库的方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了tp5(thinkPHP5)操作mongoDB数据库的方法。分享给大家供大家参考,具体如下:

1.通过composer安装

composer require mongodb/mongodb

2.使用

<&#63;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相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

脚本宝典总结

以上是脚本宝典为你收集整理的tp5(thinkPHP5)操作mongoDB数据库的方法全部内容,希望文章能够帮你解决tp5(thinkPHP5)操作mongoDB数据库的方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。