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

网络编程 2021-07-05 08:24www.168986.cn编程入门
这篇文章主要介绍了tp5(thinkPHP5)操作mongoDB数据库的方法,结合实例形式简单分析了mongoDB数据库及thinkPHP5连接、查询MongoDB数据库的基本操作技巧,需要的朋友可以参考下

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

1.通过poser安装

poser 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框架的PHP程序设计有所帮助。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by