Descending order in Firebase database

There is a 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('ups')

      

How can I query and order them instead of upvotes

DESC?

+3


source to share


1 answer


Unfortunately firebase does not allow descending descending. I usually just change the order of the returned results on the client side.

If your data request is too large to be sorted on the client side, you can do



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

      

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

+4


source







All Articles