Jump to content
ludo1960

PHP Mongodb driver connection and insert

Recommended Posts

Posted (edited)

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

Mongo has its own PHP classes. Is this what you are using?

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

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.