2016-08-31 9 views
0

私はこのPHPでMongDBの更新/削除操作のステータスを取得するには?

function delete($col,$condition = array()){ 
    $bulk = new MongoDB\Driver\BulkWrite; 
    $bulk->delete($condition, ['limit' => 1]); 

    $manager = new MongoDB\Driver\Manager($this->mongo); 
    $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); 
    $result = $manager->executeBulkWrite($this->db.'.'.$col, $bulk, $writeConcern); 
    var_dump($result); 

} 

を使用していますが、コードが動作し、怒鳴る

オブジェクト(MongoDBは\ドライバーの\ WriteResult)#30(9){[ "nInserted"] => int型のような出力を取得します["nModified"] => int(0)["nRemoved"] => int(0)["nUpserted"] => int(0)[" array(4){["w"] => array(0){> writeErrors " ["wtimeout"] => int(1000)["journal"] => NULL}}

文字列(8) "大多数" ["wmajority"] => bool

オブジェクトを配列に変換できません。どのようにオブジェクトから "nRemoved"/"nUpdated"のようなプロパティを取得するには?

答えて

0

あなたはこのようにそれを行うことができます。

$bulk = new MongoDB\Driver\BulkWrite(['ordered' => false]); 
$bulk->update(
    $condition, 
    $data, 
    $n_opts 
); 
if($result = $this->mongo->executeBulkWrite($this->db.'.'.$collection, $bulk)){ 
    if(($result->getMatchedCount() > 0 AND $result->getModifiedCount() > 0) OR ($result->getMatchedCount() == 0 AND $result->getUpsertedCount() > 0)){ 
     return true; 
    }else{ 
     return false; 
    } 
}else{ 
    return false; 
} 

削除がカウント返すために:

$ result-> getDeletedCount();ここでは、この方法について

さらに詳しい情報: http://php.net/manual/en/class.mongodb-driver-writeresult.php

+0

はどうもありがとうございました!それはうまくいった。 –

関連する問題