dfdfdsfsd
dfdfdsfsd

Reputation: 327

How to order documents by timestamp automatically in firebase?

I've created an app and I am using firebase as well but I want to order the documents by timestamp such that my documents will be ordered from recently added ones to older ones.

I wonder if there is a solution rather than using orderBy method because my project is very big.

please help me.

Note: I am devolping the app using flutter & dart.

Upvotes: 2

Views: 2431

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598797

Firebase Realtime Database always returns results in ascending order. There is no way to order descending order.

This means you have two common options:

  1. Reverse the nodes on the client.
  2. Store an inverted value in a property, and order on that.

If you want to retrieve a subset of the items, keep in mind that you can combine any of these approaches with limitToFirst() and limitToLast. For example, to get the 10 most recent messages, you could:

  1. order on timestamp
  2. get the last 10 items
  3. reverse them client-side

Also see:

Upvotes: 3

Related Questions