2012-07-16 36 views
1

我在更新記錄時遇到問題。我的文檔結構是這樣的:Mongodb增加對象的一部分

"_id" : "1141825507009", 
    "person" : [ 
     { 
      "amount" : 25, 
      "id" : "1141825507009" 
     } 
] 

我想3遞增person.amount其中person.id爲1141825507009.那麼容易。 關於什麼:

Increment person.amount by 3 if person.id exist. 
Add to person list with amount 3 and id 239something845 if person.id does not exist in list. 
+0

是兩個單獨的操作或單個要麼/或行動? – 2012-07-16 12:23:49

+0

你正在使用哪個驅動程序? – Aaron 2012-07-16 12:49:06

+0

@ BryceAtNetwork23我正在使用PHP驅動程序。我想在一個動作中這樣做:addtoset,而不是設置值增加它。 – ewooycom 2012-07-16 13:15:17

回答

0

我想你想要的是https://jira.mongodb.org/browse/SERVER-340。你可能想看/投票。

如果您不需要原子做到這一點,你可以這樣做:

db.foo.update({"person.id" : {$exists : false}, /* other criteria */}, 
    {$inc : {"person.amount":3}}); 
var result = db.runCommand({getLastError:1}) 
if (result.n == 0) { // nothing updated, person.id must exist 
    db.foo.update({/* criteria */}, 
     {$addToSet : {person : {amount:3, id:"239something845"}}}); 
} 
+0

所以它還沒有實現。謝謝! – ewooycom 2012-07-16 14:55:25