mongodbslimjenssegers-mongodb

Using Jenssegers MongoDB with Slim and Capsule


I am using the Slim 4 framework along with Jenssegers MongoDB library and Capsule (Illuminate\Database from Laravel). I have got the MongoDB extension installed on my Linux server and everything seems ok connection wise, but I cannot seem to insert data into the database or get anything from it. I have tried with the query builder and Eloquent. My code with query builder example is below.

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',
]);

The database and collection exist as I can see them in Compass. Can anyone see where I'm going wrong with the code or is it a configuration issue?


Solution

  • There were two problems, one was the driver missing as you point out, but the other was the fact my PHP is running in Linux using Vagrant and it was pointing to localhost, but the MongoDB server is running on the Windows machine and not Linux. Thanks.