MongoDB
webman默认使用 jenssegers/mongodb 作为mongodb组件,它是从laravel项目中抽离出来的,用法与laravel相同。
使用jenssegers/mongodb之前必须先给php-cli安装mongodb扩展。
使用命令
php -m | grep mongodb查看php-cli是否装了mongodb扩展。注意:即使你在php-fpm安装了mongodb扩展,不代表你在php-cli可以使用它,因为php-cli和php-fpm是不同的应用程序,可能使用的是不同的php.ini配置。使用命令php --ini来查看你的php-cli使用的是哪个php.ini配置文件。
安装
composer require psr/container ^1.1.1 illuminate/database jenssegers/mongodb ^3.8.0
配置
在 config/database.php 里增加 mongodb connection, 类似如下:
return ['default' => 'mysql','connections' => [...这里省略了其它配置...'mongodb' => ['driver' => 'mongodb','host' => '127.0.0.1','port' => 27017,'database' => 'test','username' => null,'password' => null,'options' => [// here you can pass more settings to the Mongo Driver Manager// see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use'database' => 'admin', // required with Mongo 3+],],],];
示例
<?phpnamespace app\controller;use support\Request;use support\Db;class User{public function db(Request $request){Db::connection('mongodb')->collection('test')->insert([1,2,3]);return json(Db::connection('mongodb')->collection('test')->get());}}
