Notice: Undefined property: MongoDB\\Driver\\Manag

2019-02-26 02:51发布

问题:

I want to run mongo from php I am using MongoDB 64 bit, WampServer 64 bit, Apache Version: 2.4.18 and PHP Version: 5.6.19. I also added mongodb.dll extension in php.ini of PHP as well as in php.ini of apache. I have also installed Composer and run the command

composer require "mongodb/mongodb=^1.0.0"

On running the code below I am getting this error:

Connection to mongo is successful and db is also selected then why this error?

   require 'vendor/autoload.php';
   // connect to mongo
   $m = new MongoDB\Driver\Manager();
   echo "Connection to database successfully";

   // select a db
   $db = $m->mydb;
   echo "Database mydb selected";

?>

I also tried $db = $m->test; but getting the same error. Thanks in advance for any help.

回答1:

Try

new MongoClient()

If you still get the error then it Looks like it can't load the Mongo Module.

First You have to download the stable dll zip file. PHP-MONGO

Download the Threaded-version of the module. Copy the dll file and paste it in C:\wamp64\bin\php\php5.6.19\ext (if wamp). then go to your php.ini file and paste the following script: extension=php_mongo.dll in the extensions row.

Note: There shouldn't be a ';' before the extension . Then restart WAMP. In the wamp-php-extension menu you could find the php_mongo. tick mark it. Restart Wamp.

Start mongo and the try executing the following script.

<?php
    if($connection = new mongoClient()){
        echo "Connected Successfully";
     } 
?>`

Note: First start the Mongo server.

For more info: PHP/Mongo Manual

If you have problem in start Mongo then Write the command as follows in your Mongo/bin directory:

mongod --storageEngine=mmapv1 --dbpath [your-path]

your-path= The path you want to save the Databse files. (ex: F:\data\db )

The Mongo sever would be running now. Don't close the cmd.



回答2:

The problem is resolved by adding

$m = new MongoDB\Client("mongodb://localhost:27017");

instead of

$m = new MongoDB\Driver\Manager();

Actually, I was calling the wrong class. :)