Jump to content
ludo1960

PHP Mongodb driver connection and insert

Recommended Posts

Hello guys,

I've installed the mongoDB PHP driver using "sudo pecl install mongodb"

Testing shows driver is installed correctly and it appears nicely in phpinfo():

ludo@aegir:~$ php7.2 -i | grep -i mongodb
mongodb
MongoDB support => enabled
MongoDB extension version => 1.5.3
MongoDB extension stability => stable
mongodb.debug => no value => no value 

For connecting I've got:

   $db = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 

Now I want to define a collection and insert a record, i've been using code from here  https://www.php.net/manual/en/set.mongodb.php:

$collection = $db->mydatabase->mytable;

$result = $collection->insertOne( [ 'name' => 'test', 'item' => 'testitem' ] );

But this fails  Notice: Undefined property: MongoDB\Driver\Manager::$mydatabase in /var/www....

Has anyone got a workingg example of defining and inserting into mongoDB. Seems the driver was updated fairly recently and so stuff on the internet is not always relevant. 

Edited by ludo1960

Share this post


Link to post
Share on other sites

unfamiliar with Mongodb so this may be a silly question but what is this line saying:

 $db = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 

Is "MongoDB\Driver\Manager"  a function name that is part of the mongodb interface?   To me it looks more like a path (without quotes) so I am puzzled.

Share this post


Link to post
Share on other sites
3 hours ago, ginerjm said:

unfamiliar with Mongodb so this may be a silly question but what is this line saying:


 $db = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 

Is "MongoDB\Driver\Manager"  a function name that is part of the mongodb interface?   To me it looks more like a path (without quotes) so I am puzzled.

Sorry, the link is here https://www.php.net/manual/en/set.mongodb.php there is our chum MongDB\Driver\Manager .

Share this post


Link to post
Share on other sites

It looks to me like you want client not manager. In any case have you verified the path? You should be checking $db to make sure it is set.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.