Setting an existing counter


I can set a new value in a counter or set the value to a new counter but how can I set the value of an existing counter ?


Counters are just stored as ASCII strings, so you just need to set it to the string encoded equivalent of the number you’d like it to be set to.

Are you using 1.3 or 2.0? I can point you to a more specific method.



I’m using 2.0. I’ll appreciate an example.


You can do this by using an overload that takes an “initial” parameter:

var res = bucket.Increment("thekey", 1, 100);

The first parameter is the key, the second is the delta, which is the amount of change to apply to the counter, and the third is the initial value to be set. So, the first time this is called “thekey” will have a value of 100, the second time it will have a value of 101 and so forth.

To set the value of an existing key, you need to use Increment or Decrement with a delta that when Incremented or Decremented will match the value you wish to set the counter to. For example:

var res2 = bucket.Increment("thekey", 1000);
var res3 = bucket.Decrement("thekey", 1000);

In this case, assuming the “key” was initially 12 and you wanted to set it to 1012, you would just increment up by the difference. You can use the same approach to go back down the counter to set it back to 12 by using Decrement.




Thanks @jmorris,

I know how to inc or dec or create new.
What I don’t know is how to set a value of an existing key if I don’t know the current value, in just one call. I can’t assume an initial value


Hi Itay,
I hope that i understand your question correctly.
The last parameter to Increment/decrement is only used if the key not already exists. If the key already exists the last parameter is ignored.
the sample below will increment the counter from 100 to 109 (ignoring the decrement of the initialization value 100 - 0, 100 - 1, 100-2 etc.)

for (ulong i = 0; i < 10; i++)
var res = bucket1.Increment(“counter”, 1, 100-i);



Assume the counter exist and its value is unknown.
How can I set it to 10 if I don’t know his value ?


Ahhh, well the count is just af key/value pair as any other document.
You could just use Upsert, like this:

var res = bucket1.Upsert(“counter”, “10”).Value;
for (ulong i = 0; i < 10; i++)
var res2 = bucket1.Increment(“counter”, 1, 100-i).Value;

this would make the code above count from 10 and +1 for each loop iteration.


Thanks but this kind of contradicts this

Note: The key you are incrementing or decrementing should be created by calling the Increment() or Decrement() themselves, not by using one of the other creational operations, such as Insert() or Upsert(). The reason is that the server stores the value as the ASCII representation and not as a 64-bit integer.


@itay -

It does, but it will work. What won’t work is if you create the initial value as a long, which is what most people do and end up failing:

var res = bucket1.Upsert("counter", 10).Value;

This should fail!



In this case, I suggest adding this to the docs