Firestore createId()编号未按时间顺序排列 [英] Firestore createId() ids not in chronological order

查看:49
本文介绍了Firestore createId()编号未按时间顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firebase实时数据库中,自动生成的ID会将所有条目按创建时的时间顺序保留.Firestore似乎是完全随机的.有没有一种方法可以像在数据库中生成ID一样创建ID?还是我需要通过创建时间戳并以此排序?

In Firebase realtime database, autogenerating ids keeps all entries in chronological order of when they were created. Firestore seems to be completely random. Is there a way to create ids the same way they're generated in database? Or do I need to pass a creation timestamp and sort by this?

推荐答案

来自 Firestore添加数据的文档:

重要:与Firebase实时数据库中的推送ID"不同,Cloud Firestore自动生成的ID不提供任何自动排序.如果您希望能够按创建日期对文档进行排序,则应将时间戳记存储为文档中的字段.

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有关更新数据的文档:

So you indeed will need to add a timestamp field to the documents if you want to order chronologically. You can use server-side timestamps for that, as shown in the Firestore documentation on updating data:

docRef.update({
  timestamp: firebase.firestore.FieldValue.serverTimestamp()
});

因此,您可以在控制台中以及在自己的查询中按时间顺序进行排序.

With that you can order chronologically both in the console and in your own queries.

这篇关于Firestore createId()编号未按时间顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆