2012-07-16 35 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