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.
A query with
endkeybut notstartkeywill 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
limitorendkeyvalue (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.
The results will be in reversed (descending!) order. You can either reverse them in your client (it’s only five rows) or write a
_listfunction in CouchDB to reverse them before sending a response.