5

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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.