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

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

问题描述

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

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
  }
]

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

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

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

我该如何通过upvotes DESC查询和订购它们?

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

推荐答案

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

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天全站免登陆