Skip to content

Commit bc091c3

Browse files
Merge pull request #238 from couchbase/DOC-10990-4.0
update counters
2 parents 63a04d3 + c5b9b16 commit bc091c3

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

modules/concept-docs/pages/documents.adoc

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,40 @@ var res = await collection.get('user:kingarthur', {
103103
104104
include::{version-server}@sdk:shared:partial$documents.adoc[tag=counters1]
105105
106-
[source,java]
106+
[source,javascript]
107107
----
108-
// Java example:
109-
String counterDocId = "counter-doc";
108+
var counterDocId = "counter-doc";
110109
// Increment by 1, creating doc if needed
111-
collection.binary().increment(counterDocId);
110+
const result = await collection.binary().increment(counterDocId, 1, {
111+
initial: 1000
112+
});
112113
// Decrement by 1
113-
collection.binary().decrement(counterDocId);
114-
// Decrement by 5
115-
collection.binary().decrement(counterDocId,
116-
DecrementOptions.decrementOptions().delta(5));
114+
const result = await collection.binary().decrement(counterDocId, 1);
117115
----
118116
119-
include::{version-server}@sdk:shared:partial$documents.adoc[tag=counters2]
117+
In the preceding example, a counter is created by using the `counter` method with an `initial` value.
118+
The initial value is the value the counter uses if the counter document doesn't yet exist.
119+
120+
Once created, the counter can be incremented or decremented atomically by a given _amount_ or _delta_.
121+
Specifying a positive delta increments the value and specifying a negative one decrements it.
122+
When a counter operation is complete, the application receives the current value of the counter, after the increment.
123+
124+
Couchbase counters are limited to a 52-bit unsigned integer, Javascript's maximum safe integer.
125+
Many SDKs limit the _delta_ argument to the value of a _signed_ 64-bit integer.
120126
121-
[source,python]
127+
<<expiry,Expiration>> times can also be specified when using counter operations.
128+
129+
xref:howtos:concurrent-document-mutations.adoc[CAS] values aren't used with counter operations since counter operations are atomic.
130+
The intent of the counter operation is to simply increment the current server-side value of the document.
131+
If you want to only increment the document if it is at a certain value, then you may use a normal `get` function coupled with an increment:
132+
133+
[source,javascript]
122134
----
123-
# Python example:
124-
rv = cb.get('counter_id')
125-
value, cas = rv.value, rv.cas
126-
if should_increment_value(value):
127-
cb.upsert('counter_id', value + increment_amount, cas=cas)
135+
const result = await collection.get(counterDocId);
136+
value = result.value
137+
if (shouldIncrementValue){
138+
const result = await collection.binary().increment(counterDocId, incrementValue);
139+
}
128140
----
129141
130142
include::{version-server}@sdk:shared:partial$documents.adoc[tag=counters3]

0 commit comments

Comments
 (0)