tp5(thinkPHP5)怎么操作mongoDB数据库

作者:袖梨 2022-06-24

本文介绍了tp5(thinkPHP5)操作mongoDB数据库的教程,给有需要的朋友参考一下。

1.通过composer安装

composer require mongodb/mongodb

2.使用

<?php
/**
 * @author: jim
 * @date: 2017/11/17
 */
namespace appindexcontroller;
use thinkController;
use MongoDBDriverManager;
use MongoDBCollection;
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';
  }
}

相关文章

精彩推荐