ThinkPHP5下使用Mongodb


ThinkPHP5下使用Mongodb

1,下载Composer
https://getcomposer.org/download/

Composer 是PHP用来管理依赖(dependency)关系的工具。你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件。

2,安装Thinkphp-mongo组件

// 修改镜像地址,要不然可能会卡住不动
composer config repo.packagist composer https://packagist.phpcomposer.com
// 安装
composer require topthink/think-mongo

源码会被自动下载到 vendor\topthink\think-mongo 目录

3,在conf目录下创建config.php

return [

'mongodb' => [
'type' => '\think\mongo\Connection',
'hostname' => '127.0.0.1',
'database' => 'your_db',
'username' => 'your_username',
'password' => 'your_password',
'charset' => 'utf8',
'hostport' => 27017

]

];

4,使用

use think\mongo;

$collection_name = "collection";
$conn = new mongo\Connection(Config('mongodb'));
$conn->name($collection_name);
$conn->setTable($collection_name);

附composer常用命令:

// 取消指定的packlist

composer config -g –unset repos.packagist

// 更新(获取)依赖

composer update

// 可用的镜像

composer config repo.packagist composer https://packagist.laravel-china.org

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注