1.通过composer安装

composer require mongodb/mongodb

tp5操作mongo

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';
    }

}

tp5操作mongo



本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/7850916.html,如需转载请自行联系原作者

相关文章:

  • 2022-12-23
  • 2021-07-07
  • 2021-04-25
  • 2021-08-11
  • 2021-05-26
  • 2021-10-13
  • 2022-01-11
猜你喜欢
  • 2022-01-23
  • 2021-11-27
  • 2021-08-25
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案