2017-01-08 3 views
1

最近、PHP7およびMongoDBドライバからPHP 5.5および古いMongoドライバからアップグレードしました。私はすべての関数をupdate()からupdateOne()およびupdateMany()とremove()関数に変更しました。updateOneを実行しているときにMongoDB PHPドライバのエラーが発生しました

ただし、アプリケーションが初めてupdateOneエラーを実行しようとすると、エラーが発生します。 DB接続コードは次のようになります。 私はエラーを受信して​​いましたら、私はテストとして、コンストラクタで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; 
     } 

私は取得していますエラーはこれです:

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 $ this-> connection-> MyApp-> settingsの中に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 
) 

何が間違っている任意のアイデア?ありがとう!

答えて

0

コードは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); 

完全なサンプル・コードは、下方に設けられています

関連する問題