In the Meteor leaderboard example there is this line:
Players.update(Session.get("selected_player"), {$inc: {score: 5}});
How can I gracefully decrement the same field? Sadly, there is no $dec.
In the Meteor leaderboard example there is this line:
Players.update(Session.get("selected_player"), {$inc: {score: 5}});
How can I gracefully decrement the same field? Sadly, there is no $dec.
MongoDB allows you to increment by negative values:
$inc: {score: -1}
From the MongoDB docs (linked to from the Meteor docs): The $inc update operator accepts positive and negative values. A negative value effectively decrements the specified field.
http://docs.mongodb.org/manual/reference/operator/update/inc/
You can apply this code:
$inc: {
score: -1
}
apply negative values (-1, -3, -5 and so on ) for decrement value.
There is no need of something like $dec.
A negative value effectively decrements the specified field. For example using (-value
):
Players.update(Session.get("selected_player"), {$inc: {score: -value}});
Players.update(Session.get("selected_player"), {$inc: {score: -1}});
you can simply use negative value (ex : -1, -2, -3, .....) for decrement value in mongo collection
Use the $inc
update operator to decrement a field's value by providing a negative value.
db.products.updateOne(
{ name: "Laptop" },
{ $inc: { quantity: -5 } }
)
db.test.update({id: "zxf"}, {$inc: {intValue: NumberInt(-1)}});
Update update = new Update().inc(field, -1);
© 2022 - 2024 — McMap. All rights reserved.