Querying views with compound keys in couchbase-lite

I’m having an issue querying a view that as a compound key (of 2 items). The url I am constructing is returning the entire view, but what I want is a subset of that view. From the docs I understand that I need to specify a startKey and endKey as url parameters.

Its seems that the startKey should be an array of just 1 item.

The endKey should be an array of 2 items, the 1st being the same as my startKey and the 2nd being an empty object {}. Correct?

So I now have json arrays in my url as parameters. Should they be url encoded? It doesn’t seem to matter if I do or don’t, I still get the full view back. Is a ‘GET’ the wrong method?

I guess my question is this: how do I construct an http request to query a view with a compound key?

Can you post some sample source for your view (map function) and how you are querying it?

Btw some docs on querying compound keys are here:


Second is the handling of compound (array) keys. When a view’s keys are arrays, it’s very common to want to query all the rows that have a specific value (or value range) for the first element. The start key is just a one-element array with that value in it, but it’s not obvious what the end key should be. What works is an array that’s like the starting key but with a second object appended that’s greater than any possible value. For example, if the start key is (in JSON) [“red”] then the end key could be [“red”, “ZZZZ”] … because none of the possible second items could be greater than “ZZZZ”, right? Unfortunately this has obvious problems. The correct stop value to use turns out to be an empty object/dictionary, {}, making the end key [“red”, {}]. This works because the sort order in views puts dictionaries last.

Thanks for the reply. I’ve read the docs extensively but can’t find instructions on how to construct an http request that queries a view with a compound key.

My map function looks something like this:

    function (doc) {
       emit([doc.group_id, doc.client_timestamp], doc);

And I’ve tried various ways of querying the view. Such as:


This doesn’t work. I assume because its not a valid URL (the query string parameters aren’t encoded correctly). So if I encode them (using encodeURIComponent()) I end up with this:


Which doesn’t work either. Maybe the encoding is wrong? Just one working example would help. I’ve looked at the java code that I supposed to decode these parameters but can’t figure out quite what the url is supposed to look like (both startled and end key get parsed into Object.class).

Could you supply a url that would work for the example in the docs that you posted? Or point me to any one example of it working. I’m completely stuck!

Any advice would be greatly appreciated.

Hi, I’ve got it working. The second form of URL was correct (with the http-encoded json arrays). There was a bug in the react-native-couchbase-lite API that was breaking the IOS http stack somehow. I’ll try and get it fixed.

An example query would have still been useful though, I couldn’t find any in the docs.

1 Like

@nick-couchbase can you explain how you got that working. I am having the same problem. My view is:

map : function(doc) {
	if (doc.type == "Contact" && doc.party && doc.contacPurpose) {
		emit([doc.party, doc.contacPurpose],doc)

then I do this request:


I get 0 results even though I have the document in the database:

  "offset": 0,
  "total_rows": 24,
  "rows": []

This is the correct url that fixed my issue:


I used the ng-couchbase-lite angular wrapper:

var key = ‘[“126490”, “BILLING_LOCATION”]’;
hossDatabase.queryView(config.designDoc,“ContactsByPurpose”,{ key: key }).then(function(res){