前提:由於缺省驅動程序以單向消息方式工作(只要不使用),更新語句是無害的。mongodb批量插入的微優化?或者這是一個重要的優化?
問題以下片段是在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,自$
運營商需要的查詢部分的查詢字段吸更新。 amirite? iamrite?
如果這是做事情的最佳方式,我可以在我的代碼中獲得創意,並堅持更新操作?
編輯:新形式
有我的邏輯錯誤,謝謝蓋茨。還是想摺疊如果可能的更新: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)。
你是對的,這是非法的。'c'應該是集合中的一個可變元素:/。 – 2012-02-03 09:39:53