MongoDB
webman uses mongodb/laravel-mongodb as the default MongoDB component. It is extracted from the Laravel project and works the same way as Laravel.
You must install the MongoDB extension for php-cli before using mongodb/laravel-mongodb.
Note
Use the commandphp -m | grep mongodbto check if the MongoDB extension is installed forphp-cli. Note: Even if you have installed the MongoDB extension forphp-fpm, it does not mean you can use it inphp-cli, becausephp-cliandphp-fpmare different applications and may use differentphp.iniconfigurations. Use the commandphp --inito see whichphp.iniconfiguration file yourphp-cliis using.
Installation
composer require -W webman/database mongodb/laravel-mongodb ^4.8
A restart is required after installation (reload is not sufficient).
Configuration
Add the mongodb connection in config/database.php, similar to the following:
return [
'default' => 'mysql',
'connections' => [
...other configurations omitted here...
'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
'appname' => 'homestead'
],
],
],
];
Example
<?php
namespace app\controller;
use support\Request;
use support\Db;
class UserController
{
public function db(Request $request)
{
Db::connection('mongodb')->table('test')->insert([1,2,3]);
return json(Db::connection('mongodb')->table('test')->get());
}
}
Model Example
<?php
namespace app\model;
use DateTimeInterface;
use support\MongoModel as Model;
class Test extends Model
{
protected $connection = 'mongodb';
protected $table = 'test';
public $timestamps = true;
/**
* @param DateTimeInterface $date
* @return string
*/
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format('Y-m-d H:i:s');
}
}