Laravel installs jenssegers/laravel-mongodb extensions and pits encountered

Keywords: Database MongoDB PHP Laravel

mongoDB was used in the project, so you went to the largest gay dating website and found the most stars jenssegers/laravel-mongodb

Package Installation

1. Based on README, find the package version corresponding to the local laravel

2. Introduce using composer requir ing in development environment

3. Configure config/app.php

        /*
         * mongoDB
         */
        Jenssegers\Mongodb\MongodbServiceProvider::class,

4. Configuration of database.php

"mongodb" => [
      "driver" => "mongodb",
      "host" => "127.0.0.1",
      "port" => 27017,
      "database" => "data",
      "username" => "test",
      "password" => "test",
   ],

Note: If mysql is not involved in the project, you can directly change the default data engine above to mongodb without specifying the env method.

'default' => env('DB_CONNECTION', 'mysql'),

5. Use in Code

// get data
        $mongo = \DB::connection("mongodb")
            ->collection($collection)
//            ->where("****","***")
            ->first();

problem

1,unsupported driver [mongodb]

tip1 Checks if phpinfo contains mongoDB extensions. If mongoDB extensions are not available, install them yourself and try again
tip2 Focus!Be sure to check the laravel log file
After checking mongoDb extensions, they are still not supported. Check the log:

Type error: Argument 3 passed to MongoDB\Driver\Server::executeQuery() must be an instance of MongoDB\Driver\ReadPreference or null, array given {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Argument 3 passed to MongoDB\\Driver\\Server::executeQuery() must be an instance of MongoDB\\Driver\\ReadPreference or null, array given at F:\\project\\interface_center_jtl\\vendor\\mongodb\\mongodb\\src\\Operation\\Find.php:299)

Trace to the specific location, modify the return here, print again, and find that the get/first method is working properly.The insert method works the same way, but it modifies InsertMany in the same directory

!! The right way to solve this problem from the source, check out phpinfo, upgrade mongo to the latest stable version!Check out my previous articles centos source installs mongodb extensions above php7 If it is windows, download the dll file directly

2,Authentication failed.

tip1 checks the password account and connects with the tool to see if it's configured incorrectly?

tip2 uses native methods for testing, and if native methods can connect but package methods cannot, then consider the package problem

 // %40 is the escape of the @ symbol
 $manager = new \MongoDB\Driver\Manager("mongodb://mongo:user:passwd%40abcdef@127.0.0.1:27017");
 $query=new \MongoDB\Driver\Query([]);
 $cursor = $manager->executeQuery('test.test', $query);
 dd($cursor);

/ As above, I found that I still can't connect. Consider mongo's problem. Testing again, my colleague found that the connection writing can be successfully modified!The following:

 $manager = new \MongoDB\Driver\Manager("mongodb://mongo:user:passwd%40abcdef@127.0.0.1:27017/data");

Under Best Forbidden, I compare the versions of mongoDB and find that the test environment is actually an old one./Manual ejection

After communicating with Operations and Maintenance and Testing, we decided to make code compatible in order to maintain the stability of the online version and the flexibility of agile development.
Open the package source again and find the following details (tap on the blackboard: watch the path, it's inside the jenssegers package)

Until then, the following modifications were made in the configuration file:

So add, commint, push, finish!

Posted by RedMaster on Wed, 08 May 2019 23:36:39 -0700