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

jacklove
jacklove 原创
2023-04-02 07:26:01 2635浏览

这篇文章主要介绍了tp5(thinkPHP5)操作mongoDB数据库的方法,结合实例形式简单分析了mongoDB数据库及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 Class SoapClient not found解决方法的讲解

PHP Class SoapClient not found的解决方法

Lumen timezone 时区设置方法

以上就是tp5(thinkPHP5)操作mongoDB数据库的方法详解的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。