Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrement like $dec?

Tags:

meteor

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.

like image 917
markhorrocks Avatar asked Oct 25 '14 07:10

markhorrocks


People also ask

How do you decrement?

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).

How do you decrement in assembly language?

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.

How do you decrement variables?

Adding 1 to a variable is called incrementing and subtracting 1 from a variable is called decrementing.

How do you decrement in C++?

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.


2 Answers

MongoDB allows you to increment by negative values:

$inc: {score: -1} 
like image 160
Dan Dascalescu Avatar answered Sep 22 '22 17:09

Dan Dascalescu


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/

like image 39
bengott Avatar answered Sep 21 '22 17:09

bengott