如何使用firebase按子值降序分页? [英] How can I paginate in descending order by child value with firebase?

查看:27
本文介绍了如何使用firebase按子值降序分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些数据如下:

Let's say I have some data as follows:

{
    "name": "post #1",
    "votes": 100
},
{
    "name": "post #2",
    "votes": 10000
},
{
    "name": "post #3",
    "votes": 750
}

等等.

firebase 中的数据显然不是按票数排序,而是按键排序.

The data in firebase is not ordered by votes obviously, it's order by key.

我希望能够按票数按降序对这些数据进行分页.

我想我必须这样做:

ref.orderByChild('votes')
    .startAt(someValue)
    .limitToFirst(someOtherValue)
    .once('value', function(snapshot) {
        resolve(snapshot);
    });

或者,我可能需要使用 limitToLastendAt?

Or perhaps, I'd need to use limitToLast, and endAt?

通过 key 进行分页很容易,但这个让我很难受.

Pagination by key is quite easy, but this one is stumping me.

更新 (2017-10-16): Firebase 最近宣布了 Firestore - 他们完全托管的 NoSQL 数据存储,应该可以更轻松地执行更复杂的查询.可能仍然有一些用例,人们可能会使用他们一直提供的实时数据库.如果您是这些人中的一员,那么这个问题仍然适用.

Update (2017-10-16): Firebase recently announced Firestore - their fully managed NoSQL datastore which should make it easier to do more complex queries. There may still be some use cases where one might use the Realtime database they've always provided. If you're one of those people this question should still apply.

推荐答案

经过一整天的编码后,我发布了这个问题,我的大脑一片混乱.

I posted this question after a long day of coding and my brain was mush.

今天早上我又解决了这个问题,而且经常遇到这种情况,很快就找到了解决方案.

I took another crack at it this morning, and as is so often the case, was able to find the solution rather quickly.

解决方案:

为了对数据进行分页,我们需要能够从列表中间任意拉取已排序的数据.您可以使用 firebase API 提供的以下函数来执行此操作:

In order to paginate data, we need the ability to arbitrarily pull sorted data from the middle of a list. You can do this using the following functions provided by the firebase API:

startAt

endAt

limitToFirst

limitToLast

可以在此处找到更多信息.

就我而言,由于我需要按降序排列数据,我将使用 endAtlimitToLast.

In my case, since I need my data in descending order, I'll use endAt and limitToLast.

假设我希望我的每个页面都有五个项目.我会像这样写我的第一个查询:

Assuming I'd like each of my pages to have five items. I would write my first query like so:

ref.orderByChild('votes')
    .limitToLast(5)
    .once('value', function(snapshot) {
      console.log('Snap: ', snapshot.val());
    });

这将获得最高票数的 5 个项目.

This gets the 5 items with the highest number of votes.

为了获得下一页,我们需要存储来自上次查询结果的条数据.我们需要存储投票数,以及投票数最少的项目的键.也就是说,我们列表中的最后一项.

In order to get the next page, we'll need to store two pieces of data from the results of our last query. We need to store the number of votes, and key for the item that had the least number of votes. That is to say, the last item in our list.

使用该数据,我们的下一个查询和所有后续查询将如下所示:

Using that data, our next query, and all subsequent queries, would look as follows:

ref.orderByChild('votes')
    .endAt(previousVoteCount, previousKey)
    .limitToLast(5)
    .once('value', function(snapshot) {
      console.log('Snap: ', snapshot.val());
    });

您会注意到我在此查询中添加了 endAt.这将获得从上一个列表中的最后一个开始的具有最多选票的接下来的 5 个项目.我们必须包括上一个列表中最后一项的键,以便如果有多个投票数相同的项目,它将返回一个以正确项目开头的列表,而不是它找到的第一个具有该编号的项目票数,可能位于您从初始查询中获得的列表中间的某个位置.

You'll notice in this query I added endAt. This gets the next 5 items with the most votes starting from the last one in the previous list. We must include the the key for the last item in the previous list so that if there are multiple items with the same number of votes, it'll return a list beginning with the correct item, and not the first one it finds with that number of votes, which could be somewhere in the middle of the list you got from the initial query.

就是这样!

这篇关于如何使用firebase按子值降序分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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