Setting consistency when using upsert function


#1

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


#2

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


#3

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.


#4

@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!


#5

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.


#6

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.