0

I have a small database in Firebase realtime database which contains posts added by users. Everytime I open the console, each and every post is in the correct position.

I'm trying to migrate to Cloud Firestore. I've created a script to copy every post objects from Firebase realtime database to Cloud Firestore and works fine. The problem is when I switch the tab to Firestore, the posts are not ordered anymore and I find hard time to find a post.

How can I order the posts in the exact same way that were in Firebase and also in the ListView?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Hans Spigel
  • 167
  • 2
  • 8

1 Answers1

2

Strange Firestore console situation

It is not strange, is the normal sorting scheme provided (by default) in Firebase console.

Unlike the Fireabse realtime database ids, Cloud Firestore ids are actually purely random. There's no time component included. That is why (by default) there is no order. However, as Frank van Puffelen mentioned in his comment:

The Cloud Firestore console support sorting/filtering of the documents on any field (since June 2018), by clicking the "filter" icon above the document list.

enter image description here

Beside that, if know the id of a document, just simply CTRL + F in your browser and you'll be able to find the desired document very easy. In my opinion, this sorting scheme makes it relatively intuitive to find the document you might be looking for.

If you want to order your elements in your ListView, you should add those requirements into queries. The order in your query is not related with order you see in the dashboard.

Those ids are required so an app like yours can work perfectly on a big scale. At a big scale, it doesn't matter what order the documents appear in the console because the console is useless for viewing large amounts of documents.

A solution for ordering your posts in the ListView, would be to order your posts according to a timestamp property.

For Android, here you can find how to add the date using a model class or FieldValue.serverTimestamp().

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • The Cloud Firestore console support sorting/filtering of the documents on any field (since June 2018), by clicking the "filter" icon above the document list. See https://firebase.googleblog.com/2018/06/sort-and-filter-in-firestore-console.html – Frank van Puffelen Nov 01 '18 at 15:32
  • @FrankvanPuffelen P.S. Congrats for the Firebase summit! Firebase team was great, you were great! Thanks for doing this! – Alex Mamo Nov 02 '18 at 06:09