2016-06-12 7 views
0
$document = $client->$db->$collection->findAndModify(
    [ $field => $value ], // query 
    ['$set' => $updatedDocument], // update 
    null, // only return these fields 
    [ 
    "sort" => [], 
    "remove" => false, 
    "update" => [], // array for update? not sure what this does! 
    "new" => false, 
    "upsert" => false 
    ] 
); 

を削除し、それが条件付きにinsertupdateまたはremove文書DBに追加のクエリを作成することなく、可能ですか?例えば条件付きで、挿入、更新またはその<a href="http://php.net/manual/en/mongocollection.findandmodify.php" rel="nofollow">syntax for the PHP Driver</a>で文書

$document = $client->$db->$collection->findAndModify(
    [ $field => $value ], // query 
    // if $document != $newDocument: update 
    ['$set' => $updatedDocument], 
    null, // only return these fields 
    [ 
    "sort" => [], 
    // if $document != $certainCondition: remove 
    "remove" => true, 
    "update" => [], 
    // if $document does not exist: insert (or upsert?) 
    "new" => false, 
    // if $document does not exist: upsert? 
    "upsert" => false 
    ] 
); 
+0

'options'はキー配列でなければなりません。 '[" update "=> [...]]' – jszobody

+0

のように@jszobodyああ、訂正しました。 – 3zzy

答えて

2

findAndModifyのみ見つかった文書を更新または削除することができます。 removeフラグをtrueに設定し、更新フィールドを同時に空でないものに設定すると、ドキュメントは見つけられ、更新され、次に削除されます。どのドライバを使用するかは問題ではありません。それはmongodbの構築方法です。

プレーン・アップデートを使用して削除する必要があります。

+0

ありがとうございます。しかし、あなたが 'insert'、' update'、または 'remove'の3つのアクションを*実行することを許可していますが、本当に条件をチェックすることはできません。とにかく、今はfindとinsert/update/removeを使うだけです。 – 3zzy

+0

これは、find + removeのfind + updateのための原子置換として使用されることになっています。 –

関連する問題