Using now() in startkey and endkey

I have view based on the timestamp index key (year, month, day). When I try to run the query, I can specify startkey=[2016,3,1] and endkey=[2016,3,10] and it returns the records between 3/1 and 3/10. How can I use today’s date without tying the date itself. This is what I am trying to do:

http://host:8092/some-bucket/_design/audit/_view/bySystemDate?connection_timeout=60000&limit=10000&skip=0&reduce=false&endkey=[now()-20d]

Is it possible to do? it is not working for me.

Thanks

Are you using the SDK’s ViewQuery object? You would have to construct your endkey yourself using a Calendar and creating the correct JsonArray out of it (by adding the year, month and day fields of the Calendar into the JsonArray). You can then set the json array instance as the parameter for ViewQuery's endKey(JsonArray) method.

I am using dateToArray method and I can use the hardcoded date like startkey=[2016,3,1] and endkey=[2016,3,10] in the url.
But problem is how can I get the today’s date without typing it specifically for startkey and endkey in the url. I would like to have it dynamically such as now() – 20.

dateToArray() and now() are javascript functions that you can use in the map function, which does the indexing, but not reference when querying.

Don’t build the URL yourself, there’s no way to express what you want directly to the view (which doesn’t have a function interpreter for query time and would just think that “now() - 20” is just another string key).

Use the SDK, and Java, to dynamically create the equivalent of dateToArray(now() - 20) (using Calendar and JsonArray as briefly explained before) then to query the view (it will internally build the URL and all that).

Refer to the documentation of the SDK if you need more information on View querying: developer.couchbase.com/documentation/server/4.1/sdks/java-2.2/querying-views.html