我正在使用Slim 4框架以及Jenssegers MongoDB库和Capsule(来自Laravel的Illuminate \ Database)。我在Linux服务器上安装了MongoDB扩展,一切似乎都可以正常连接,但是似乎无法将数据插入数据库或从中获取任何东西。我已经尝试使用查询生成器和Eloquent。我的带有查询构建器示例的代码如下。
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->getDatabaseManager()->extend('mongodb', function($config, $name) {
$config['name'] = $name;
return new \Jenssegers\Mongodb\Connection($config);
});
$capsule->addConnection([
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'testing',
'username' => '',
'password' => '',
], 'mongodb');
// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
$capsule->connection('mongodb')->collection('testing')->insert([
'test1'=>'hello',
'test2'=>'world',
]);
数据库和集合存在,正如我在Compass中看到的那样。谁能看到我在哪里出错了代码,或者这是配置问题?
有两个问题,一个是您指出的缺少驱动程序,另一个是我的PHP在Linux上使用Vagrant运行并且它指向localhost的事实,但是MongoDB服务器在Windows计算机上运行,而没有Linux。谢谢。