Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB - Querying array key value for first key element only

Tags:

view

couchdb

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?

like image 604
Gopherkhan Avatar asked Jul 24 '12 02:07

Gopherkhan


1 Answers

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
like image 159
Alex B Avatar answered Sep 20 '22 20:09

Alex B