如何通过 Firebase 控制台将文档添加到 Cloud Firestore 文档列的底部

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

我正在通过 Firebase 控制台将数据添加到云 Firestore。我添加了一个集合以及相应的文档和字段。当我单击“添加文档”时,新文档随机出现(据我所知)在文档列中。我希望新生成的文档出现在文档栏的底部。在应用程序上查看数据时,顺序很重要。数据用于回收者视图。这可能吗?

Cloud Firestore 不以与 RTDB 相同的方式对文档进行排序。自动 ID 与时间无关。您将需要添加一个时间戳字段并按此字段对您的数据进行排序。

你可以阅读这篇文章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.

我使用了 Firestore 的 "set" 方法而不是 "add" 并使用了数字日期字符串;它会自动添加并按数字顺序列出它们。

// Swift

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

如果您使用 "add" 方法,将使用随机字母数字字符串自动生成密钥。

集合按字母数字顺序排列所有内容,因此仅使用数字或字母键设置文档将允许有序列表。