Search code examples
viewcouchdb

CouchDB - Querying array key value for first key element only


I have a couchdb view set up using an array key value, in the format:

[articleId, -timestamp]

I want to query for all entries with the same article id. All timestamps are acceptable.

Right now I am using a query like this:

?startkey=["A697CA3027682D5JSSC",-9999999999999]&endkey=["A697CA3027682D5JSSC",0]

but I would like something a bit simpler.

Is there an easy way to completely wildcard the second key element? What would be the simplest syntax for this?


Solution

  • First, as a comment pointed out, there is indeed a special value {} that is ordered after any value, so your query becomes:

    startkey=["target ID"]&endkey=["target ID",{}]
    

    This is as equivalent to a wildcard match.

    As a side note, there is no need to reverse the ordering in the map function by emitting a negative timestamp, you can reverse the order as an option to the view invocation (your start and end key will be swapped).

    startkey=["target ID",{}]&endkey=["target ID"]&descending=true