flutter
flutter

Reputation: 6766

how to add documents to bottom of the document column in Cloud Firestore through the Firebase console

I'm adding data to cloud Firestore through the Firebase Console. I've added a collection and the corresponding documents and fields. When I click on " ADD DOCUMENT", the new document appears randomly (as far as I can make out) on the document column. I want the newly generated document to appear at the bottom of the document column. The order matters when viewing the data on the app. The data is used in a recycler view. Is this possible?

Upvotes: 1

Views: 1411

Answers (2)

andrewcar
andrewcar

Reputation: 422

I used Firestore's "set" method instead of "add" and used a numerical date string; it is automatically adding and listing them in numerical order.

// Swift

let dataToSave: [String: Any] = ["example": "example"]
collectionRef.document(dateString).setData(dataToSave) { (error) in
}

If you use the "add" method, the key will be automatically generated with a random alphanumeric string.

The collection orders everthing alphanumerically, so setting your documents using keys of numbers or letters, exclusively, will allow for an ordered list.

Upvotes: 0

Jason Berryman
Jason Berryman

Reputation: 4898

Cloud Firestore does not order documents in the same way as the RTDB. Auto IDs are not time related. You will need to add a timestamp field and order your data by this field.

You can read about this here.

Important: Unlike "push IDs" in the Firebase Realtime Database, Cloud Firestore auto-generated IDs do not provide any automatic ordering. If you want to be able to order your documents by creation date, you should store a timestamp as a field in the documents.

Upvotes: 2

Related Questions