CouchDB 中的分页? [英] Pagination in CouchDB?

查看:15
本文介绍了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.

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

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

推荐答案

CouchDB Guide 有很好的讨论分页,包括很多示例代码,在这里:http://guide.couchdb.org/草稿/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,这些参数将毫无用处.实际上,CouchDB 会首先查看 startkey 参数,然后如果多个潜在的凝视行具有相同的键,它将使用 startdocid 参数进一步重新定义范围的开始但不同的文档 ID.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天全站免登陆