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.
The decrement operator is represented by two minus signs in a row. They would subtract 1 from the value of whatever was in the variable being decremented. The precedence of increment and decrement depends on if the operator is attached to the right of the operand (postfix) or to the left of the operand (prefix).
The DEC instruction is used for decrementing an operand by one. It works on a single operand that can be either in a register or in memory.
Adding 1 to a variable is called incrementing and subtracting 1 from a variable is called decrementing.
Increment ++ and Decrement -- Operator as Prefix and Postfix In programming (Java, C, C++, JavaScript etc.), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With