Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB query with endkey parameter

Tags:

couchdb

I am trying to implement the previous page functionality for pagination, and I thought that using the endkey parameter would return the previous page's rows where the last row would equal the endkey. But is it even possible to do a query using ONLY the endkey parameter without startkey?

For example:

http://something.com:5984/db3/_design/app/_view/a_view?limit=5&endkey=["ABC","6L","201112"]&descending=false

When I run this query, the last row's key is not equal to the endkey I specified. Instead, it seems like CouchDB just grabs the first 5 rows in the view and completely ignores the endkey parameter.

like image 748
Richard Avatar asked Aug 26 '11 15:08

Richard


1 Answers

A query with endkey but not startkey will implicitly work like &startkey=null. That is, CouchDB will start from the very first key on the very first row and continue until it reaches the endkey.

CouchDB always starts the response from its startkey, and stops the response from its limit or endkey value (whichever comes first).

To fetch the last 5 rows, you need to scan backwards (descending) and then your startkey is right where you need it.

?limit=5&startkey=["ABC","6L","201112"]&descending=true

The results will be in reversed (descending!) order. You can either reverse them in your client (it's only five rows) or write a _list function in CouchDB to reverse them before sending a response.

like image 79
JasonSmith Avatar answered Sep 25 '22 16:09

JasonSmith