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?

1
  • 6
    I believe all you need to do is ?startkey=["A697CA3027682D5JSSC"]&endkey=["A697CA3027682D5JSSC",{}] Commented Jul 24, 2012 at 3:19

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

Excellent. I was searching all over for this, but I guess my terminology was not exact. Thanks very much :)
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 it's been too long, so I don't remember anymore. I think it had something to do with alphabetical ordering of {}.
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"}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.