Error while Upserting Documet

I have a scenario, where multiple documents need to be updated with a certain array of texts.
Now I am running a for loop to fetch the documents first and update their value. Then the loop will pick the second document, like so. As this list may contain duplicate ids too (it’s our use case :slight_smile: ) suppose in the second iteration the same document comes in, so we first try to read it and then again upsert it.

Below is a rough code for the same -

 let cluster = await couchbase.connect(config.serverUrl, {
            username: config.username,
            password: config.password
        });
 const bucket = cluster.bucket(bucketName);
 const dbCollection = bucket.defaultCollection();

  for (const replaceObject of dataToReplace) {
     //readTheDocumentUsingID();
     //doSomething();
     const replacedObjectCB = await dbCollection.upsert(id, alteredObject);
  }

Now, the problem is when the same document is read in the second iteration of the loop, the document is still not updated in all the nodes by the first iteration, resulting in reading the old document.
Thus sometimes we are not able to preserve the changes done in the first round, and the document is getting overwritten by the second round.

Earlier we were using raw N1ql queries, where adding returning in the query would ensure updates to all nodes.

We read about Durability Level, which will allow us to update all nodes while upserting, but that also didn’t work.

const replacedObjectCB = await dbCollection.upsert(id, alteredObject.content, 
                         { durabilityLevel: 2 });

Any Idea how we can fix this?

If you’ve awaited on the operation before doing the second loop, you should always read your own writes. Other than if there are node failures during your operations, durability doesn’t enter into it.

Ah, unless you’re saying you are doing a query (which I don’t see in the code, depending on what you mean by readTheDocumentUsingId()). For that, if you want your query to be updated with respect to any recent mutations, you’ll want to pass the options for scan consistency, which I think is something like this…

  const qo: QueryOptions = { scanConsistency: QueryScanConsistency.RequestPlus  }

Then pass that in as options on your query.

1 Like