在CouchDB中分页吗? [英] Pagination in CouchDB?

查看:313
本文介绍了在CouchDB中分页吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何实施分页所需的查询?

How would I go about implementation the queries required for pagination?

基本上,当请求页面1时,获取前5个条目.对于第2页,获取下5个,依此类推.

Basically, when page 1 is requested, get the first 5 entries. For page 2, get the next 5 and so on.

我打算通过ouchdb-python模块使用它,但这不会对实现产生任何影响.

I plan to use this via the couchdb-python module, but that shouldn't make any difference to the implementation.

推荐答案

CouchDB指南进行了很好的讨论分页,包括许多示例代码,请点击此处: http://guide.couchdb.org/draft/recipes.html#pagination 这是他们的算法:

The CouchDB Guide has a good discussion of pagination, including lots of sample code, here: http://guide.couchdb.org/draft/recipes.html#pagination Here's their algorithm:

  • 从视图中请求rows_per_page + 1
  • 显示rows_per_page行,将最后一行存储为next_startkey
  • 作为页面信息,保留startkeynext_startkey
  • 使用next_*值创建下一个链接,并使用其他值创建前一个链接
  • Request rows_per_page + 1 rows from the view
  • Display rows_per_page rows, store last row as next_startkey
  • As page information, keep startkey and next_startkey
  • Use the next_* values to create the next link, and use the others to create the previous link

N.B .:在CouchDB中获取页面的正确方法是指定开始键,而不是您可能认为的开始索引.但是,您如何知道开始第二页的键?聪明的解决方案:您不是请求一个页面10行,而是请求11行,但是只显示10行,并使用第11行中的值作为下一页的开始键."

N.B.: The proper way to fetch pages in CouchDB is by specifying a starting key, not a starting index like you might think. But how do you know what key to start the 2nd page on? The clever solution: "Instead of requesting 10 rows for a page, you request 11 rows, but display only 10 and use the values in the 11th row as the startkey for the next page."

如果希望多个文档发出相同的键,则除了startkey之外,还需要使用startdocid来正确分页.原因是单独的startkey将不再足以唯一标识一行.如果不提供startkey,则这些参数将无用.实际上,如果多个潜在的凝视行具有相同的键但具有不同的文档ID,则CouchDB首先将查看startkey参数,然后将使用startdocid参数进一步重新定义范围的开始. enddocid也是如此.

If you expect to have multiple documents emit identical keys, you'll need to use startdocid in addition to startkey to paginate correctly. The reason is that startkey alone will no longer be sufficient to uniquely identify a row. Those parameters are useless if you don't provide a startkey. In fact, CouchDB will first look at the startkey parameter, then it will use the startdocid parameter to further redefine the beginning of the range if multiple potential staring rows have the same key but different document IDs. Same thing for the enddocid.

这篇关于在CouchDB中分页吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆