I'm new to CouchDB, but I think I can help. I read the following from CouchDB: The Ultimate Guide:
One of the drawbacks of the linked page list style is that ... going to a specific page really doesn't work ... If you really need to go to a page across the entire spectrum of documents ... you can still save the integer value index as an index views and have a hybrid approach when solving pagination.
- http://books.couchdb.org/relax/receipts/pagination
If I read this right, the approach in your case will be as follows:
- Insert a numerical sequence into your document set.
- Extract this number sequence into the number representation index.
- Use arithmetic to calculate the correct numeric keys of the beginning and end for your arbitrary page.
For step 1, you need to add something like "page_seq" as a field for your document. I have no specific recommendation on how you get this number, and I'm curious to know what people think. For this scheme to work, it must increase exactly by 1 for each new record, so there are probably no RDBMS sequences (those I know may skip numbers).
For step 2, you should write a view with a display function, something like this (in Javascript):
function(doc): emit(doc.page_seq, doc)
For step 3, you should write your query something like this (assuming the page and page numbering sequences begin with 1):
results = db.view("name_of_view") page_size = ... # say, 20 page_no = ... # 1 = page 1, 2 = page 2, etc. begin = ((page_no - 1) * page_size) + 1 end = begin + page_size my_page = results[begin:end]
and then you can iterate through my_page.
A clear drawback to this is that page_seq assumes that you are not filtering the dataset for your view, and you will quickly run into a problem if you try to get this to work with an arbitrary query.
Comments / improvements are welcome.
Owen S.
source share