Idan
Idan

Reputation: 432

MongoDB PHP Driver Error when doing updateOne

I have recently upgraded to PHP7 and to the MongoDB driver from PHP 5.5 and the old Mongo driver. I have changed all the functions from update() to updateOne() and updateMany(), as well as the remove() functions.

However, the first time my application attempts to perform an updateOne error, I am getting an error. The DB connection code looks like this. Please note that I have added an updateOne function in the constructor as a test once I saw I'm receiving errors :

if (!extension_loaded('mongodb')) die("MongoDB is not installed!");
        try {
            $this->connection = new MongoDB\Client('mongodb://'.$auth.self::HOST.':'.self::PORT.$authDb);
            $this->database = $this->connection->selectDatabase(self::DBNAME);

            # Test function, added due to errors

            $this->connection->WIOC->settings->updateOne(array('_id' => 'xxx'), array('$set' => array('test' => 'yes')));
        } catch (MongoConnectionException $e) {
            throw $e;
        }

The error I'm getting is this:

Fatal error: Uncaught Error: Call to undefined method MongoDB\Driver\BulkWrite::updateOne() in /Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Operation/Update.php:140 Stack trace: 0 /Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Operation/UpdateOne.php(77): MongoDB\Operation\Update->execute(Object(MongoDB\Driver\Server)) 1 /Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Collection.php(828): MongoDB\Operation\UpdateOne->execute(Object(MongoDB\Driver\Server)) 2 /Users/Idan/Sites/MyApp/include/mongoConnect.php(30): MongoDB\Collection->updateOne(Array, Array) 3 /Users/Idan/Sites/MyApp/include/mongoConnect.php(40): DBConnection->__construct() 4 /Users/Idan/Sites/MyApp/include/framework.php(6): DBConnection::instantiate() 5 /Users/Idan/Sites/MyApp/index.php(3): require('/Users/Idan/Sit...') 6 {main} thrown in /Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Operation/Update.php on line 140

var_dump of $this->connection->MyApp->settings shows a MongoDB\Collection, so I assume I'm using the appropriate newer driver. Even stranger, here is the list of methods for the object:

Array
(
    [0] => __construct
    [1] => __debugInfo
    [2] => __toString
    [3] => aggregate
    [4] => bulkWrite
    [5] => count
    [6] => createIndex
    [7] => createIndexes
    [8] => deleteMany
    [9] => deleteOne
    [10] => distinct
    [11] => drop
    [12] => dropIndex
    [13] => dropIndexes
    [14] => find
    [15] => findOne
    [16] => findOneAndDelete
    [17] => findOneAndReplace
    [18] => findOneAndUpdate
    [19] => getCollectionName
    [20] => getDatabaseName
    [21] => getManager
    [22] => getNamespace
    [23] => insertMany
    [24] => insertOne
    [25] => listIndexes
    [26] => replaceOne
    [27] => updateMany
    [28] => updateOne
    [29] => withOptions
)

Any idea what is wrong? thanks!

Upvotes: 1

Views: 3178

Answers (1)

Alex Goncharenko
Alex Goncharenko

Reputation: 1066

The code you have provided makes use of legacy MongoDB driver and hence is not compatible with the new one.

Provided you have actually installed the newest driver, the proper way to perform any kind of updates (update/insert/delete) is achieved through the usage of MongoDB\Driver\Manager::executeBulkWrite method.

The complete sample code is provided below:

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update([ '_id' => 'xxx' ], [ '$set' => [ 'test' => 'yes' ]]);
$manager->executeBulkWrite('DB.Collection', $bulk);

Upvotes: 2

Related Questions