“上一个”链接 - 相当于 LIMIT x OFFSET y?

发布于 2024-09-27 03:25:39 字数 673 浏览 2 评论 0原文

我正在使用 CouchDB 创建一个页面系统,显示:

  • 每页 10 个项目
  • 到上一页的链接(如果有)
  • 到下一页的链接(如果有)

来自 这篇文章 关于该主题,我知道使用 skip 不是最佳选择,我应该使用 startkey 属性指定第一个文档,从那里读取 11 个文档,显示前 10 个文档,并使用第 11 个 的键显示下一页的链接。让我烦恼的是上一页的链接。文章说:

填充上一页的链接就像将当前的开始键带到下一页一样简单。如果没有之前的开始键,我们就在第一页。

这在转到下一页时有效:当我从第 4 页移至第 5 页时,我可以记得上一页是 4。但是当我从第 5 页移回第 4 页时,我无法继续下去 第 3 页的 startkey。这是如何工作的?

是否可以(并推荐)使用 endkey 以及 skip=10limit=1 来查找上一页上的第一个元素,这样我就可以创建一个返回它的链接?

I'm creating a page system using CouchDB, showing:

  • 10 items per page
  • a link to the previous page (if any)
  • a link to the next page (if any)

From this article on the topic, I understand that using skip is suboptimal, and that I should instead use the startkey property to specify the first document, read 11 documents from there, display the first 10 and use the key of the 11th to display the link to the next page. What troubles me is the link to the previous page. The article says:

Populating the link to the previous page is as simple as carrying the current startkey over to the next page. If there’s no previous startkey, we are on the first page.

This works when going to the next page: when I move from page 4 to page 5 I can remember that the previous page was 4. But when I move back from page 5 to page 4, I have no way of carrying over the startkey of page 3. How can this work?

Is it possible (and recommended) to use endkey along with skip=10 and limit=1 to find the first element on the previous page, so that I may create a link back to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2024-10-04 03:25:39

事实上,您只能请求 11 个文档,而不能跳过,这就是 Futon 所做的(查看 CouchDB 日志)。

技巧

下一页和上一页链接都是相似的:startkey 是第一个或最后一个元素,使用 skip=1 以避免重叠。然后,您必须正确使用 descending 参数来获取上一个文档或下一个文档。

执行

每当您请求页面时,CouchDB 都会以 11 个文档进行响应。假设第一个的键是first,最后一个的键是last。分页链接将如下所示:

"next": /db/_view/myview?descending=true&limit=11&startkey=last&skip=1
"back": /db/_view/myview?descending=false&limit=11&startkey=first&skip=1

Et voilà!当 descendingfalse 时,您只需在显示文档之前反转文档即可。 (CouchDB 指南中的“使用视图查找数据”很好地解释了这些参数之间的关系和 B 树。)

额外的好处是,

您可以轻松获取第一页或最后一页的 docid(limit=1descending true 或 false),并获得一个分页系统看起来很像经典数据库中的东西(第一个、最后一个、上一个、下一个)。

You can in fact only ask for 11 documents with no skip, and that is what Futon does (look at CouchDB logs).

The trick

Both the next and previous page link will be similar: startkey is the first or last element, with a skip=1 to avoid overlapping. You then have to correctly use the descending parameter to get previous documents or next documents.

The execution

Whenever you're asking for a page, CouchDB answers with eleven documents. Let's say the key of the first one is first and the key of the last one is last. The pagination links will look like:

"next": /db/_view/myview?descending=true&limit=11&startkey=last&skip=1
"back": /db/_view/myview?descending=false&limit=11&startkey=first&skip=1

Et voilà! You just have to reverse the documents before displaying them when descending is false. ("Finding your data with views" from the CouchDB guide explains nicely the relation between those parameters and B-Trees.)

Bonus

You can easily get the docid of the first or last page (limit=1 and descending true or false), and get a pagination system that looks a lot like something you would have with a classical database (first, last, previous, next).

海风掠过北极光 2024-10-04 03:25:39

阅读 21 篇文档,而不是 11 篇 - 向前阅读 1 篇文档,向后阅读 10 篇文档。第一个是打开上一页的钥匙。

Read 21 documents instead of 11 - One extra going forward, and ten going backwards. The very first one holds the key to the previous page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文