About query view with startkey and endkey



I am working on a phonegap application using CBL 1.4.1
i have create a view like following
[10, 20], John
[13, 23], Sam
[15, 25], May

and query with Rest API startkey=[0, 0]&endkey=[20, 24]
the return is all rows. but what i expect is only John and Sam.
it is okey with same query on Sync Gateway. but just don’t work on CBL.

i test like startkey=[10, 0]&endkey=[10, 24]
it return correctly for John and Sam. so the view should be working.

it seems like stop checking at the first range.

I search all topic here, but seems no one have same problem.
I think I may doing some thing wrong.


You’re asking for a range of keys, where the primary key (1st array index) goes from 0 to 20, Since all of your docs have a primary key from 0 to 15, this returns all documents. I’m not sure what you’re trying to do in your query – do you want “primary in 0…20 and secondary in 0…24”? There’s no way to do that with a regular index; it would take something like an r-tree. (Yes, you can do this in SQL, but it requires multiple index lookups internally, the equivalent of multiple view queries.)


Yes, i want to do search with “primary in 0…20 and secondary in 0…24”. which matching both condition.

I tested on Sync Gateway/Couchbase Server, it can do this kind of querying. I thought CBL can also do.

Anyway, thanks for your reply.


Server can do this with N1QL queries (not the older map/reduce style.)
Couchbase Lite 2.0 has N1QL-compatible queries and can do this kind of thing too.

To do this in 1.x, query for just the primary key range, using startKey=[0] and endKey=[20,{}] (the empty dictionary acts like a wild-card; see the docs.) That will return all the docs with the primary key in range. Then iterate through the results testing them to see if the secondary key is in range, and keep the ones where it is.