本文介绍了如何通过firebase的儿童价值降序排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

等等。

Firebase中的数据并不是通过投票明显排序的,而是按键排序的。



我希望能够按照票数按降序排列这些数据。



我想我必须这样做:

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

或者,我需要使用 limitToLast endAt



分页由很容易,但是这个是我的绊脚石。

更新(2017-10-16): Firebase近日宣布 - 完全托管的NoSQL数据存储,可以使复杂的查询更容易。可能还有一些用例可能会使用他们一直提供的实时数据库。如果你是其中一个人,这个问题应该仍然适用。

解决方案

我的大脑是糊涂的。



今天早上我又一次发现了这个问题,就像往常一样,能够很快找到解决方案。



解决方案:为了分页数据,我们需要能够从中间任意地拉取排序的数据名单。您可以使用firebase API提供的以下功能执行此操作:

startAt



endAt



limitToFirst



limitToLast





在我的情况下,因为我需要我的数据降序,我将使用 endAt limitToLast 5个项目。

我会写我的第一个查询是这样的:

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

得到5个项目的最高的票数。 p>

为了得到下一页,我们需要从最后一个查询的结果中存储两个段的数据。我们需要存储票数,而且票数最少的项目也是关键。也就是说,我们列表中的最后一项。

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

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

在这个查询中你会注意到我添加了 endAt 。这将得到从前一个列表中的最后一个开始票数最多的下5个项目。我们必须在上一个清单中包含最后一个项目的关键字,这样如果有多个具有相同票数的项目,它将返回一个以正确项目开头的清单,而不是它找到的第一个清单的投票,这可能是在你从最初的查询列表中间的某处。

就这样!

Let's say I have some data as follows:

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

And so on.

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

I'd like to be able to paginate this data by the number of votes in descending order.

I think I'd have to do something like:

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

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

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

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.

Solution:

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

More information can be found here.

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());
    });

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());
    });

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.

That's it!

这篇关于如何通过firebase的儿童价值降序排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:16