【发布时间】: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
)
知道有什么问题吗?谢谢!
【问题讨论】: