How exactly do startkey and endkey work in CouchDB?

I work with a location database in CouchDB. I created a view where my key is an array with rounded latitude and longitude values. Now I select the following conditions:

Startkey: [ 52.34, 4.883 ] Endkey: [ 52.37, 4.903 ] 

Here I expect that I will receive only documents where the latitude is between 52.34 and 52.37. And the longitude is between 4.883 and 4.903.

As a result, I get:

 [ 52.358, 4.919 ] [ 52.358, 4.919 ] [ 52.362, 4.861 ] [ 52.362, 4.861 ] [ 52.362, 4.861 ] 

As you may have noticed, in the first result the longitude is greater than the longitude of the final key, 4.919.

Now I know / read somewhere that I get some values ​​that are outside the range of the second element of the array. But how is it possible that the first element no longer meets the criteria?

I searched a little Google and I cannot find an explanation about the startkey / endkey file as an array. Who can give me an explanation of how CouchDB iterates over documents and determines when to "start" and "finish"?

+7
source share
1 answer

taking the following data in your view with these keys:

startkey == [a, 11] and endkey == [c, 11] :

 [a, 10] [a, 11] <-- startkey [a, 12] <-- [b, 10] <-- [b, 11] <-- [b, 12] <-- [c, 10] <-- [c, 11] <-- endkey [c, 12] 

(All marked with an arrow will be returned).

Data in the view is sorted using the key. With startkey and endkey you can control where to start and end the presentation. You cannot specify data limits. Anything sorted between startkey and endkey will be returned. Please read http://wiki.apache.org/couchdb/View_collation for more information.

If you want to perform geospatial queries, you should check out GeoCouch ( https://github.com/couchbase/geocouch/ ).


Summed up: Keys in CouchDB views are stored in one-dimensional lists. Entries in these lists are sorted according to the rules in View_collation . Two-dimensional arrays may look special, but in reality this is not so. [a, 10] sorted after [a] and after a and before [b, 5] and before [c] (for example).

If you use startkey and endkey , you say "everything, including after startkey , and before and after endkey ". The startkey and endkey do not have to be in the list.

+15
source

All Articles