Bucket upsert returns before insert operation completes




I have used upsert to add document. But it returns before insertion operation completes. I have also tried to set “persist_to” but it also not working.

bucket.upsert(data.id, data, {'persist_to' : 1}, function (error, result) {
if (!error && result) {
    console.log('Response Doc: ' + JSON.stringify(result));
} else {
    logger.error('Couchbase Error : ' + error);

Am I doing something wrong?
Please suggest me a solution for it.



Can anyone help me with this?


@sp111 thanks for providing the code, but can you describe what happens exactly and what you are expecting?


@daschl We have a messaging app in which we have poll type message. When one user respond to a poll, I have used upsert to insert response document.(upsert used as response can be existing) After that upsert, all other user should get real time notification about result. To calculate result, first I get all results and then prepare result from that. For that I query

 SELECT * from MyApp WHERE  type = 'message_response' AND messageId = 'id1';

But for first response this returns empty array. Expected result would be the document I have just inserted.
I have set indexes on type and messageId.
Same database we are using for PHP backend but in that if ‘persist_to’ set to 1 then we got expected result. Let me know if I am missing some configuration part.

Thank you.


Hey @sp111,

It looks like you are using N1QL to retrieve the results of your poll in this instance. Due to the nature of Couchbase, our indexes are updated asynchronously from our Key/Value store itself, this means that the index may not be immediately updated after you perform your upsert. You should use the query.consistency(couchbase.N1qlQuery.Consistency) to cause your query to wait until the index is up to date.

Cheers, Brett


Thank you very much @brett19, it’s working.