A newer version of this documentation is available.

View Latest

Atomic operations

The Node.js Couchbase SDK supports several operations that allow one-step, atomic changes to documents. These operations include counter, prepend, and append functions.

Counter operation

The counter() method enables you to implement an atomic counter as a Couchbase document. These operations expect UTF-8 encoded string representations of the number. You can either execute this operation against a previously created document, or create a new document by using the initial property passed as an option. More information about the initial property is available below.

The following example shows how to use the counter() method to increment your counter and get the value back:

$myBucket = $myCluster->openBucket();
$myBucket->insert('document_name', 10);
$res = $myBucket->counter('document_name', 1);
var_dump($res);

Sample output from the example:

object(CouchbaseMetaDoc)#4 (4) {
  ["value"]=>
  int(11)
  ["flags"]=>
  int(0)
  ["cas"]=>
  resource(5) of type (CAS)
}

The following example shows how to use the counter() method to decrement your counter and get the value back:

$myBucket = $myCluster->openBucket();
$myBucket->set('document_name', 10);
$res = $myBucket->counter('document_name', -1);
var_dump($res);

Sample output from the example:

object(CouchbaseMetaDoc)#4 (4) {
  ["value"]=>
  int(9)
  ["flags"]=>
  int(0)
  ["cas"]=>
  resource(5) of type (CAS)
}

You can additionally perform a counter operation with an initial value specified. In this case, the document will be created if it does not exist. The initial property will do nothing if the document already exists.

$myBucket = $myCluster->openBucket();
$res = $myBucket->counter('document_name', 1, array('initial'=>10));

Sample output from the example:

object(CouchbaseMetaDoc)#4 (4) {
  ["value"]=>
  int(10)
  ["flags"]=>
  int(0)
  ["cas"]=>
  resource(5) of type (CAS)
}
If a new counter document is created by using the initial property, the counter is set to the value of initial, not initial+offset.

Append and prepend operations

The append and prepend operations allow you to perform a binary append or prepend to a Couchbase document.

This operation is performed without any consideration for data type, thus appending a JSON document to a JSON document will result in an invalid object (for instance: {}{}).

The following is an example of how to use the append() method to add further text to an already existing document:

$myBucket = $myCluster->openBucket();
// Insert a starting document
$myBucket->insert('document_name', 'Beautiful');
// Append some content to it
$myBucket->append('document_name', 'World!');
// Prepend some more text
$myBucket->prepend('document_name', 'Hello ');
// Retrieve its updated contents
$res = $myBucket->get('document_name');
var_dump($res);

Sample output from the example:

object(CouchbaseMetaDoc)#4 (4) {
  ["value"]=>
  string("Hello Beautiful World!")
  ["flags"]=>
  int(0)
  ["cas"]=>
  resource(5) of type (CAS)
}