简体   繁体   English

更新时MongoDB PHP驱动程序错误

[英]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. 我最近已从PHP 5.5和旧的Mongo驱动程序升级到PHP7和MongoDB驱动程序。 I have changed all the functions from update() to updateOne() and updateMany(), as well as the remove() functions. 我已经将所有功能从update()更改为updateOne()和updateMany(),以及remove()函数。

However, the first time my application attempts to perform an updateOne error, I am getting an error. 但是,我的应用程序第一次尝试执行updateOne错误时,出现了错误。 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 : 请注意,一旦看到错误,我已经在构造函数中添加了updateOne函数作为测试:

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 致命错误 :未捕获错误:调用/Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Operation/Update.php:140中未定义的方法MongoDB \\ Driver \\ BulkWrite :: updateOne() 0 /用户/ Idan /站点/MyApp/include/vendor/mongodb/mongodb/src/Operation/UpdateOne.php(77):MongoDB \\ Operation \\ Update-> execute(Object(MongoDB \\ Driver \\ Server))1 /用户/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}在第140行的/Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Operation/Update.php中抛出

var_dump of $this->connection->MyApp->settings shows a MongoDB\\Collection, so I assume I'm using the appropriate newer driver. $ this-> connection-> MyApp-> settings的var_dump显示MongoDB \\ Collection,所以我假设我正在使用适当的较新驱动程序。 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! 谢谢!

The code you have provided makes use of legacy MongoDB driver and hence is not compatible with the new one . 您提供的代码使用旧版MongoDB驱动程序 ,因此与 驱动程序不兼容。

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. 如果您实际上已经安装了最新的驱动程序,则可以通过使用MongoDB \\ Driver \\ Manager :: executeBulkWrite方法来实现执行任何类型的更新(更新/插入/删除)的正确方法。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM