在 Firebase 数据库中按降序排序 [英] Sorting in descending order in Firebase database

查看:26
本文介绍了在 Firebase 数据库中按降序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase 实时数据库中有以下图书列表:

<预><代码>[{标题:不要让我思考",点赞数:110},{title: "神话人物月",点赞数:111},{标题:代码完成2",点赞数:112}]

默认情况下,以下查询按 ASC 顺序返回此列表:

firebase.database().ref('books').orderByChild('upvotes')

如何通过 upvotes DESC 查询和排序?

解决方案

不幸的是,firebase 不允许按降序返回.我通常只是颠倒了客户端返回的结果的顺序.

如果您的数据查询太大而无法对客户端进行排序,您可以这样做

firebase.database().ref('books').orderByChild('ups').limitToLast(2)

然后从那里反转结果.文档可以在这里

There is this list of Books in the Firebase Realtime Database:

[
  {
    title: "Don't Make Me Think",
    upvotes: 110
  },
  {
    title: "The Mythical Man Month",
    upvotes: 111
  },
  {
    title: "Code Complete 2",
    upvotes: 112
  }
]

By default, the query below returns this list in ASC order:

firebase.database().ref('books').orderByChild('upvotes')

How can I query and order them by upvotes DESC instead?

解决方案

Unfortunately firebase doesn't allow returning by descending order. I have generally just reversed the order of the results returned client side.

If your data query is too large to sort client side you can do

firebase.database().ref('books').orderByChild('ups').limitToLast(2)

and then invert the results from there. Documentation can be seen here

这篇关于在 Firebase 数据库中按降序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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