James Allardice
James Allardice

Reputation: 165941

How do you query a CouchDB view that emits complex keys?

Given a CouchDB view that emits keys of the following format:

[ "part1", { "property": "part2" } ]

How can you find all documents with a given value for part1?

If part2 was a simple string rather than an object startkey=["part1"]&endkey=["part1",{}] would work. The CouchDB docs state the following:

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"}]

Unfortunately, the documentation doesn't offer any suggestion on how to deal with such keys.

Upvotes: 2

Views: 762

Answers (1)

pegli
pegli

Reputation: 690

The second element of your endkey value needs to be an object that collates after any possible value of the second element of your key. Objects are compared by property-by-property (for example, {"a":1} < {"a":2} < {"b":1}) so the best way to do this is to set the first property name in your endkey to a very large value:

startkey=["part1"]&endkey=["part1", { "\uFFF0": false }]

The property name of \uFFF0 should collate after any other property names in the second key element, and even works when the second element is an empty object or has more than one property.

Upvotes: 2

Related Questions