There is no document in view after the document has been inserted in bucket.
My document:
{
"state": 1,
"rdbms_id": 0,
"startDate": 1511882685998,
"endDate": -1,
"type": "kv",
"userid": 1222,
"uuid": "84fd36ad-b8bd-4abb-90ac-eae407f9364a",
"amount": 1234,
"source_id": 12
}
View index code:
function (doc, meta) {
if(meta.type=="json"){
if(doc.type && doc.type === "kv"){
if(doc.startDate && doc.startDate<=Date.now()){
if(doc.endDate && (doc.endDate>=Date.now() || doc.endDate==-1)){
if(doc.state && doc.state==1){
emit([doc.userid,doc.source_id], null);
}
}
}
}
}
}
My view query:
curl http://Administrator:pass@localhost:8092/bss_write/_design/fihrist/_view/forUcrKVIds?limit=6&stale=false&connection_timeout=60000&inclusive_end=true&skip=0
View is updated when adding document with curl.
curl -H "Content-Type: application/json" -X POST -v -d 'Jsondocument' 'http://Administrator:pass@localhost:8091/pools/default/buckets/bss_write/docs/kv-84fd36ad-b8bd-4abb-90ac-eae407f9364a'
But the view is not updated when you add it with the program.
Cluster cluster = CouchbaseCluster.create("localhost");
cluster.authenticate("username", "");
Bucket bucket = cluster.openBucket("bss_write");
JsonObject doc = JsonObject.fromJson(Jsondocument);
bucket.upsert(JsonDocument.create(type+"-"+uuid, doc));
Do I need to run a procedure to programmatically update the view?
Update 2:
The problem is the time difference. When the document is added, the view is not updated because the date condition is not matched.
However, the view is not updated even if the view conditions are met after the document is added.
For example:
My computer timestamp is 1512054982454.
Couchbase server timestamp is 1512054876554.
function (doc, meta) {
if(meta.type=="json"){
if(doc.type && doc.type === "kv"){
---->if(doc.startDate && doc.startDate<=Date.now()){ //This returns false.
if(doc.endDate && (doc.endDate>=Date.now() || doc.endDate==-1)){
if(doc.state && doc.state==1){
emit([doc.userid,doc.source_id], null);
}
}
}
}
}
}