Document update not working when retrieve using N1QL query and update it using java SDK api

I am using java sdk (java-client.2.7.6.jar) to update the existing documents in the couchbase.
If i retrieve the document and update the same using java sdk is working fine. But when retrieve document from N1QL query and update it using java sdk is not working.
I am not seeing exceptions. I am getting new CAS value as well in the couchbase upsert api response.
Has anyone faced this issue?
Also, please help me on how to trace this upsert request in couchbase server logs.

Welcome to Couchbase forums @Raghav !

If you are getting a new CAS value, that should mean that the upsert was a success (CRUD Document Operations Using the Java SDK with Couchbase Server | Couchbase Docs). Having said that, it will help us if you show the code snippet you are using for this exercise. If you can also provide the server and sdk client logs, we can triage it better. What is the server version you are working with?

You should be able to enable tracing for your code using Java SDK 2.7. There are some examples here to do the same: Tracing from the Java SDK with Couchbase Server | Couchbase Docs.

Thanks for your reply.
One interesting thing i found is, the changes which happened by sdk upsert command is successful and visible when i query the same document using SDK but the same changes are not visible when i query the document using N1Q1 query…something fishy here…
==SELECT * from bucket where type=‘Object’ and (objectState = ‘1’ or objectState = ‘2’) and objectExpiration < $currEpochTime LIMIT $maxlimit"

couchbase server version->6.6.1-9213

N1Q1 Query Code snippet:
===================== doing select * based on expired time
N1qlQueryResult queryResult=null;
Bucket bucket= getBucket(bucketType);
if(placeholderValues != null) {
N1qlParams params =;
ParameterizedN1qlQuery query = N1qlQuery.parameterized(qry, placeholderValues, params);

		queryResult = bucket.query(query);

tried both ways as stated below.

  1. JsonObject doc = JsonObject.fromJson(dtoJson);
    JsonDocument jsonDoc = JsonDocument.create(objectId, doc);
  2. jsonDocuments = Observable
    .flatMap(doc →
    Observable upsert = getBucket(bucketType).async().upsert(doc);
    return upsert;

SDK debug logs:.

2021-04-17 21:29:02.956 Operations over threshold: [ {
“top” : [ {
“operation_name” : “upsert”,
“server_us” : 165,
“last_local_id” : “637D4040ADACDB30/FFFFFFFFF8860DC4”,
“encode_us” : 630,
“last_local_address” : “XXXXXX:58978”,
“last_remote_address” : “XXXXXX:11210”,
“last_dispatch_us” : 6564,
“last_operation_id” : “0x2b”,
“total_us” : 60364
} ],
“service” : “kv”,
“count” : 1
} ]

Sounds like it might be a scan consistency issue. If you want to read your own writes, it’s necessary to specify a scan consistency other than the default, since the default prioritizes performance over consistency.

1 Like

Yes, with Couchbase you don’t have to wait on each write until the indexes are updated. Instead this is done asynchronously, and you choose at the point of the N1QL read how consistent with recent changes you need the indexes to be. E.g. you pay only for what you use.

1 Like

Thanks alot, with above “scan-consitency”, it worked for me