在Meteor排行榜的例子中有这一行:
Players.update(Session.get("selected_player"), {$inc: {score: 5}});
我怎样才能优雅地减少相同的字段?可悲的是,没有$ dec。
答案 0 :(得分:34)
Increment by -1?
$inc: {score: -1}
答案 1 :(得分:4)
来自MongoDB文档(链接到Meteor文档): $ inc update运算符接受正值和负值。负值有效地减少指定的字段。
http://docs.mongodb.org/manual/reference/operator/update/inc/
答案 2 :(得分:0)
db.test.update({id: "zxf"}, {$inc: {intValue: NumberInt(-1)}});
Update update = new Update().inc(field, -1);
答案 3 :(得分:0)
不需要$ dec之类的东西。
负值可有效减少指定字段。例如,使用({-value
):
Players.update(Session.get("selected_player"), {$inc: {score: -value}});