【问题标题】:MongoDB PHP Driver Error when doing updateOne执行 updateOne 时出现 MongoDB PHP 驱动程序错误
【发布时间】:2017-05-23 03:28:40
【问题描述】:

我最近从 PHP 5.5 和旧的 Mongo 驱动程序升级到 PHP7 和 MongoDB 驱动程序。我已将所有函数从 update() 更改为 updateOne() 和 updateMany(),以及 remove() 函数。

但是,当我的应用程序第一次尝试执行 updateOne 错误时,我收到了错误消息。数据库连接代码如下所示。 请注意,一旦我看到收到错误消息,我已经在构造函数中添加了一个 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;
        }

我得到的错误是:

致命错误:未捕获的错误:调用未定义的方法 MongoDB\Driver\BulkWrite::updateOne() 在 /Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Operation/Update.php:140 堆栈跟踪:0 /Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Operation/UpdateOne.php(77): MongoDB\Operation\Update->execute(对象(MongoDB\Driver\Server)) 1 /Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Collection.php(828): MongoDB\Operation\UpdateOne->执行(对象(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} 抛出 /Users/Idan/Sites/MyApp/include/vendor/mongodb/mongodb/src/Operation/Update.php 上线 140

$this->connection->MyApp->settings 的

var_dump 显示了 MongoDB\Collection,所以我假设我正在使用适当的较新驱动程序。 更奇怪的是,这里是对象的方法列表:

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
)

知道有什么问题吗?谢谢!

【问题讨论】:

    标签: php mongodb


    【解决方案1】:

    您提供的代码使用了legacy MongoDB driver,因此与new one 不兼容。

    如果您实际安装了最新的驱动程序,则通过使用MongoDB\Driver\Manager::executeBulkWrite 方法可以正确执行任何类型的更新(更新/插入/删除)。

    完整的示例代码如下:

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

    【讨论】:

      猜你喜欢
      • 2016-12-07
      • 1970-01-01
      • 2016-08-12
      • 2021-05-08
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 2015-08-30
      相关资源
      最近更新 更多