我正在使用Slim 4框架以及Jenssegers MongoDB库和Capsule(来自Laravel的Illuminate\database(。 我已经在我的 Linux 服务器上安装了 MongoDB 扩展,一切似乎都可以连接明智,但我似乎无法将数据插入数据库或从中获取任何东西。 我已经尝试过使用查询构建器和雄辩。 我的查询生成器代码示例如下。
use IlluminateDatabaseCapsuleManager as Capsule;
$capsule = new Capsule;
$capsule->getDatabaseManager()->extend('mongodb', function($config, $name) {
$config['name'] = $name;
return new JenssegersMongodbConnection($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 IlluminateEventsDispatcher;
use IlluminateContainerContainer;
$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',
]);
数据库和集合存在,正如我在指南针中看到的那样。 任何人都可以看到我的代码出了问题还是配置问题?
有两个问题,一个是你指出的驱动程序丢失,另一个是我的PHP使用Vagrant在Linux中运行,它指向本地主机,但MongoDB服务器运行在Windows机器上而不是Linux上。 谢谢。