当前位置: 移动技术网 > IT编程>开发语言>PHP > tp5(thinkPHP5)操作mongoDB数据库的方法

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

2018年01月25日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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';
  }
}

更多关于thinkphp相关内容感兴趣的读者可查看本站专题:《thinkphp入门教程》、《thinkphp模板操作技巧总结》、《thinkphp常用方法总结》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《zend framework框架入门教程》及《php模板技术总结》。

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网