Pagination with Couchbase
If you have to deal with a large number of documents when doing queries against a Couchbase cluster it is important to use pagination to get rows by page. You can find some information in the documentation in the chapter "Pagination", but I want to go in more details and sample code in this article.
For this example I will start by creating a simple view based on the beer-sample dataset, the view is used to find brewery by country:
function (doc, meta) {
if (doc.type == "brewery" && doc.country){
emit(doc.country);
}
}
This view list all the breweries by country, the index looks like:
| Doc id | Key | Value |
|---|---|---|
| bersaglier | Argentina | null |
| cervecera_jerome | Argentina | null |
| brouwerij_nacional_balashi | Aruba | null |
| australian_brewing_corporation | Australia | null |
| carlton_and_united_breweries | Australia | null |
| coopers_brewery | Australia | null |
| foster_s_australia_ltd | Australia | null |
| gold_coast_brewery | Australia | null |
| lion_nathan_australia_hunter_street | Australia | null |
| little_creatures_brewery | Australia | null |
| malt_shovel_brewery | Australia | null |
| matilda_bay_brewing | Australia | null |
| ... | ... | ... |
| ... | ... | ... |
| ... | ... | ... |
| yellowstone_valley_brewing | United States | null |
| yuengling_son_brewing | United States | null |
| zea_rotisserie_and_brewery | United States | null |
| fosters_tien_gang | Viet Nam | null |
| hue_brewery | Viet Nam | null |
So now you want to navigate in this index with a page size of 5 rows.