0
votes

I'm having access on a CouchDB view wich emits documents having keys of two arrays of four integers like [[int, int, int, int], [int, int, int, int]]. In a concrete example those correspond to start-date and end-date of the document:

[[2017, 5, 5, 10], [2017, 7, 2, 11]]
 Y      m  d  H     Y     m  d  H

I'm able to get documents matching a period

request="localhorst/dbname/_design/a/_view/period"
request+="?key=\[\[2017,5,5,10\],\[2017,7,2,11\]\]"
curl -sX GET $request

Question: How to ignore the "hour" field H?

What if the boundaries are partially unknown? How to get all documents within a given period, like 2017-05-05 until 2017-07-02? In other words, how can I ignore the last columns of each boundary?

I tried to use startKey and endKey

request="localhorst/dbname/_design/a/_view/period"
request+="?startKey=\[\[2017,5,5\],\[2017,7,2\]\]"
request+="&endKey=\[\[2017,5,5,\{\}\],\[2017,7,2,\{\}\]\]"
curl -sX GET $request

This does not work since it gets documents with the correct lower bound but the upper bound is wrong, e.g.:

[[2017,4,5,10],[2017,7,2,12]] <- excluded,  OK
[[2017,5,5,10],[2017,7,2,12]] <- contained, OK
[[2017,5,5,11],[2017,7,2,12]] <- contained, OK
[[2017,5,5,10],[2017,8,2,12]] <- contained, ERROR
2
My first suggestion would be to try Mango.Alexis Côté
Just a tip: Why not use RFC3339 dates instead of a custom format? It's sortable, and many programs/libraries understand it natively.Flimzy
Thanks for the interesting suggestions I will keep them im mind for future development. In the current case I cannot decide about the format oder data structures.Nicolas Heimann

2 Answers

3
votes

It's not possible. If you use complex keys, you can perform partial matches with startkey and endkey by dropping items to the right of the key array only, not by dropping items internally in the key array.

Without knowing how your documents are structured, it's difficult to offer more than generic advice. I'd look to emit a single time stamp vector and use startkey and endkey to find the range, rather than trying to use the range as the key. However, this approach might not fit your model.

Otherwise, as suggested above, using Mango may be your best bet.

0
votes

You can use empty for the start keys and empty object {} as the last.

So 2017-05-05 until 2017-07-02 would be

[2017,05,05] to [2017,07,02,{},{},{}]

You can refer to this answer: couchdb search or filtering on key array