I have couchbase documents in following format
{
"docType": "scheduledJob",
"orgId": 2,
"jobs": {
"1456753078157": {
"jobName": "Job1",
"jobId": "910271049",
"fromTime": 1456752600000,
"toTime": 1456824600000,
"key": 1456753141401,
"status": "pending"
},
"1456753141401": {
"jobName": "Job2",
"jobId": "558624841",
"fromTime": 1456752600000,
"toTime": 1456821000000,
"key": 1456753141401,
"status": "pending"
}
}
}
Which have jobs scheduled. The jobs can be executed anytime which falls between fromTime and toTime. My task is to check every hour if there is any job pending for coming hour. Means the whatever the fromTime is but toTime timestamp should be greater than timestamp of 1 hour after current time. Similarly we should fetch if the fromTime is also falls from current timestamp to next hour time stamp.
I am new to couchbase. The view I have created is
function(doc, meta){
if( doc.docType && doc.docType=="scheduledJob"){
for(var key in doc.jobs){
var job = doc.jobs[key]
if(job.status == "pending") {
emit(job.fromTime+'_'+job.toTime, job);
}
}
}
}
I am sending startkey="currentTimestamp_0000000000000" and endkey="0000000000000_currentTime+1hour-timestamp"
Please help me where I am going wrong.
The startkey/endkey combination you give will in all non-trivial cases yield an empty result. Imagine (for simplicity shortened to 2 digits) the current time to be 18, and the next hour to be 19. Then your strings are:
startkey 18_00
endkey 00_19
Lexicographically, the startkey is larger than the endkey, so the interval is empty.
The simplest way appears to me to create two separate views, one emitting just the fromTime, the other the toTime. If I understand the requirements right, you would search all jobs having fromTime between now and now + 1 hr, same for toTime, and do a programmatic set union of the two results.
It might be even simpler to use just one view, and, for each scheduledJob, doing 2 emits, one of the fromTime, another for the toTime. But I never tried doing more than 1 emit on a document, so you would have to try whether it works.
Your way of converting the number values into a string looks rather error-prone to me. If a compound key is necessary, I would prefer a proper couchbase compound key, which is achieved by e.g. emit([job.fromTime, job.toTime], job)
. But I don't see an easy solution for your problem using that kind of key.