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 ) 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?