11

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?

Gopherkhan
  • 4,317
  • 4
  • 32
  • 54

2 Answers2

15

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
Alex B
  • 82,554
  • 44
  • 203
  • 280
  • Excellent. I was searching all over for this, but I guess my terminology was not exact. Thanks very much :) – Gopherkhan Jul 24 '12 at 16:59
  • Where did you find this? it works great but I was scouring documentation for the last week and this is the first place I have seen the {} mentioned. – MBillau Aug 03 '15 at 19:53
  • 1
    @MBillau it's been too long, so I don't remember anymore. I think it had something to do with alphabetical ordering of `{}`. – Alex B Aug 07 '15 at 05:51
  • @MBillau Docs link: http://docs.couchdb.org/en/1.6.1/couchapp/views/collation.html – bendtherules Jan 31 '16 at 15:49
0

For future reference, in CouchDB 3 you can use "\ufff0" instead of {}, which would be ordered after a string or number, but before an object.

From the CouchDB 3 docs:

Beware that {} is no longer a suitable “high” key sentinel value. Use a string like "\ufff0" instead.

The query startkey=["foo"]&endkey=["foo",{}] will match most array keys with “foo” in the first element, such as ["foo","bar"] and ["foo",["bar","baz"]]. However it will not match ["foo",{"an":"object"}]

jbgt
  • 1,586
  • 19
  • 24