前提: ドライバーはデフォルトで一方向のメッセージングで動作するため (getLastError
使用されない限り)、update ステートメントは無害です。
質問次のフラグメントは、大量の挿入のためにmongodbでこれを行うための最良の方法ですか? ステップ 2 と 3 を折りたたむことはできますか?
編集:古いバグのあるフォーム、以下を参照
// step 1 : making sure the top-level document is present (an upsert in the real
example)
db.test.insert( { x :1} )
// step 2 : making sure the sub-document entry is present
db.test.update( { x:1 }, { "$addToSet" : { "u" : { i : 1, p : 2 } } }, false)
// step 3 : increment a integer within the subdocument document
db.test.update( { x : 1, "u.i" : 1}, { "$inc" : { "u.$.c" : 1 } },false)
$
オペレーターは更新のクエリ部分のクエリフィールドでプライミングを必要とするため、操作3から抜け出す方法はないと感じています。アミライト?イアムライト?
これが最善の方法である場合、コードを工夫して、更新操作に夢中になることはできますか?
編集:新しいフォーム
私の論理にバグがありました、ゲイツに感謝します。可能であれば、更新を折りたたみたい:D
// make sure the top-level entry exists and increase the incidence counter
db.test.update( { x : 1 }, { $inc : { i : 1 } }, true ) --1
// implicetly creates the array
db.test.update( { x : 1 , u : { $not : { $elemMatch : { i : 1 } } } } ,
{ $push : { u : { i : 1 , p :2 , c:0} } }) -- 2
db.test.update( { x :1 , "u.i" : 1}, { $inc : { "u.$.c" : 1 } },false) --3
注 :$addToSet
この場合は、要素ごとの一致を行うため、役に立ちませんmutable
。C++ OO のビットごとの比較用語のように、配列内の要素が何であるかを表現する方法はありません。
質問は無意味ですデータモデルが間違っています。クローズするために投票してください (OP)。