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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With