Setting consistency when using upsert function

Hello Guys I am wondering if there is a way to set the data consistency when I am performing an upsert using the nodeSdk and server 4.1

Are you talking about specifying the ‘durability requirements’?

If so, you can do that with persist_to/replicate_to. See the node docs here: http://developer.couchbase.com/documentation/server/4.5/sdk/nodejs/document-operations.html

Hello Mathew,

I am talking about updating/mutating document concurrently. By using the CAS value to ensure that the update is a success.

I am not sure how to do this.

Thanks in advance.

@fernando_l thanks for the clarification. At that same link, “cas” is listed as an optional parameter to upsert,insert,replace methods. I think maybe something like:

bucket.insert(“document_id”, {“your”:“document”}, {cas: yourCase}, function(err, res) { });

You may also want to check out the SDK Bucket documentation here: http://docs.couchbase.com/sdk-api/couchbase-node-client-2.0.0/Bucket.html

Please let me know if I can help further!

1 Like

yes just one question. How do I get the cas value? I guess I need to fetch the document doing a get and then grab the value from there.

When you retrieve a document (e.g. if you use the get method), the callback method gets error and result.

From the docs here: http://docs.couchbase.com/sdk-api/couchbase-node-client-2.0.0/Bucket.html#OpCallback

“The result contains at least a cas property”

So if you have a result object, then you can just use result.cas.

1 Like