mongo-php-driver icon indicating copy to clipboard operation
mongo-php-driver copied to clipboard

Argument 3 passed to MongoDB\Driver\Server::executeQuery()

Open MNSolderstar opened this issue 8 years ago • 7 comments

Description

Type error: Argument 3 passed to MongoDB\Driver\Server::executeQuery() must be an instance of MongoDB\Driver\ReadPreference or null, array given /home/vagrant/code/vendor/mongodb/mongodb/src/Operation/Find.php:287

Environment

Laravel 5.6.18 Homestead 5.2.0

mongodb libbson bundled version => 1.8.2 libmongoc bundled version => 1.8.2 libmongoc SSL => enabled libmongoc SSL library => OpenSSL libmongoc crypto => enabled libmongoc crypto library => libcrypto libmongoc crypto system profile => disabled libmongoc SASL => enabled mongodb.debug => no value => no value

[PHP Modules] bcmath blackfire calendar Core ctype curl date dom exif fileinfo filter ftp gd gettext hash iconv igbinary imap intl json libxml mbstring memcached mongodb msgpack mysqli mysqlnd openssl pcntl pcre PDO pdo_mysql pdo_pgsql pdo_sqlite pgsql Phar posix readline Reflection session shmop SimpleXML soap sockets sodium SPL sqlite3 standard sysvmsg sysvsem sysvshm tokenizer wddx xml xmlreader xmlwriter xsl Zend OPcache zip zlib

[Zend Modules] Zend OPcache blackfire

Code Snippet

Using https://github.com/jenssegers/laravel-mongodb

return Organisation::all();

Error

Type error: Argument 3 passed to MongoDB\Driver\Server::executeQuery() must be an instance of MongoDB\Driver\ReadPreference or null, array given
/home/vagrant/code/vendor/mongodb/mongodb/src/Operation/Find.php:287

Fix

vendor/mongodb/mongodb/src/Operation/Find.php:287

Replace:

$cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, $this->createQuery(), $this->createOptions());

With:

$cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, $this->createQuery(), $this->options['readPreference']);

MNSolderstar avatar Apr 30 '18 14:04 MNSolderstar

"MongoDB extension version" appears to be missing from your phpinfo() output, but I assume you're using 1.3.3 or 1.3.4, as 1.3.3 was the version that bumped the libmongoc dependency to 1.8.2.

The error you're seeing is caused by using mongo-php-library (package: mongodb/mongodb) 1.3.0+ with an older version of the driver. If you look at mongodb/mongodb in Packagist, you'll see that it requires ext-mongodb 1.4.0+. If you ran composer update, it should have caught this for you (unless you were also using --ignore-platform-reqs).

In any event, upgrading the mongodb extension to 1.4.x should resolve this.

jmikola avatar Apr 30 '18 14:04 jmikola

I need to figure out what version of the mongo-db extension homestead has provisioned. My local interpreter is actually using mongo 1.4.3 so composer update is satisfied on update.

Ill check the provision script and get back to you. Cheers for pointing me in general direction.

[EDIT]

Yes it appears Homestead currently provisions 1.3.3. I only did a clean install 2 weeks ago so there perhaps is my issue. Not sure how long ago 1.4.0 was released. So i need to figure out how to update this for my deployments?

MNSolderstar avatar Apr 30 '18 15:04 MNSolderstar

For anyone else using Laravel Homestead and having this error you can check your mongodb extension version by doing:

<?php
echo phpversion("mongodb");

If this prints anything lower than 1.4.0 then you can update your Homestead via SSH.

sudo pecl channel-update pecl.php.net
sudo pecl install mongodb

Restart VM from terminal in your project dir:

vagrant reload

MNSolderstar avatar Apr 30 '18 16:04 MNSolderstar

I'm not familiar with Homestead, but I believe https://github.com/laravel/homestead/pull/840 is the relevant issue for it upgrading the bundled extension to 1.4.0.

Closing this out, as there is no issue with the driver/library here.

jmikola avatar Apr 30 '18 16:04 jmikola

vendor/mongodb/mongodb/src/Operation/Find.php Open this file and search $cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, new Query($this->filter, $this->createQueryOptions()), $this->createExecuteOptions());

this line replace with below code $cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, new Query($this->filter, $this->createQueryOptions()), $this->options['readPreference']);

Arfter it run the command php artisan serve Working fine

chandanrao611 avatar Jul 28 '19 07:07 chandanrao611

@chandanrao611: The createExecuteOptions() method does more than just set the read preference. It also prepares the session option, which means that your patch could inadvertently break any queries relying on sessions (e.g. within a multi-document transaction).

The root cause of this issue was due to the installed extension being too old for the library, and the OP somehow missing Composer's notification that the installed version was not adequate (since extension requirements are noted in composer.json).

https://github.com/mongodb/mongo-php-library/commit/142d24ecebbd7c60c8a8f592727aa6878515d061#diff-2bfb552f851648971366b9269219e2c6 is the commit that introduced the createExecuteOptions() method and changed the third argument to an array when session support was introduced in 1.3.0. Using a previous version of the library would be preferable to manually patching it in your project; however, the best solution would be to upgrade the extension itself to a version compatible with the library you've installed.

jmikola avatar Jul 29 '19 06:07 jmikola

vendor/mongodb/mongodb/src/Operation/Find.php Open this file and search $cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, new Query($this->filter, $this->createQueryOptions()), $this->createExecuteOptions());

this line replace with below code $cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, new Query($this->filter, $this->createQueryOptions()), $this->options['readPreference']);

@chandanrao611 solution is Working fine .I am getting same issue .I have resolved it by this solution

sameermistry2502 avatar Oct 20 '20 05:10 sameermistry2502