Couchbase update view after add doc

Hello. I try to implement such scenario:
- create a doc and insert it into couchbase;
- update a view, that gets all docs of those type.

Here is a code:

var key = "my_key";
var res = CouchbaseClient.Store(StoreMode.Add, key, JsonConvert.SerializeObject(my_object)); // adding doc
if (res) {
  CouchbaseClient.GetView("my_ddoc", "my_view").Stale(StaleMode.UpdateAfter).Reduce(true); // trying to update view
}

If I try to call them like above, on next query with stale=ok view will not return that new doc.
To get it work I need to wait some time before call GetView with Stale=UpdateAfter. ExecuteStore command with PersistTo also didn't help.

So here are my questions:
1. Is there any way to update view after adding a new doc?
2. If there's no way to do that and I need to wait: How much time I need to wait?

1 Answer

« Back to question.

Hello Skella,

The flow you have described:

1- Set with PersistTo.Master (and check that it is successful)

2- Query with stale = false

should be enough.

This is what I do in Java and it works perfectly. (sorry I am not a .Net expert ;) ... yet )
Keep in mind that observe is taking some resources to check the status of key.

So when you say that the PersistTo.Master does help, what is the status of the result?

If you want to wait, it is nearly impossible to say how much you have to wait, simply because it really depends of the cluster resources and activity on your cluster.

What is the exact use case when you need this? Testing? UI integration? (just curious)

Regards
Tug
@tgrall

I forgot to mention an important thing, that you probably know already, do not use PersistTo in all operations: it will slow down your application and consume too much resources. (same for stale=false)

I have only one "test" server in cluster. I've tried both persistTo.One and stale=false. All combinations with PersistTo and Stale=False, Stale=UpdateAfter that comes in one "query" failed. It only works when I do 2 separate queries (1 ajax query stores new doc, callback that all is Ok, then I wait for about 1-2 seconds and send another ajax query to update that view.

Now I use this for testing. I need to get that new doc in a view results as fast as possible, so after adding new doc I try to call view update and then all read queries go with Stale=Ok. I think it's better to update view once after adding a doc than do all read queries with Stale=False.