在 Firestore 文档中添加时间戳 [英] Add timestamp in Firestore documents

查看:31
本文介绍了在 Firestore 文档中添加时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Firestore 的新手.Firestore 文档说...

I'm newbie to Firestore. Firestore docs says...

重要提示:与 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.

参考:https://firebase.google.com/docs/firestore/manage-data/add-data

那么我是否必须在文档中将密钥名称创建为 timestamp?或者 created 足以满足 Firestore 文档中的上述声明.

So do I have to create key name as timestamp in document? Or created is suffice to fulfill above statement from Firestore documentation.

{
    "created": 1534183990,
    "modified": 1534183990,
    "timestamp":1534183990
}

推荐答案

firebase.firestore.FieldValue.serverTimestamp()

不管你想怎么称呼它都很好afaik.然后你可以使用 orderByChild('created').

Whatever you want to call it is fine afaik. Then you can use orderByChild('created').

我在设置时间时也主要使用 firebase.database.ServerValue.TIMESTAMP

I also mostly use firebase.database.ServerValue.TIMESTAMP when setting time

ref.child(key).set({
  id: itemId,
  content: itemContent,
  user: uid,
  created: firebase.database.ServerValue.TIMESTAMP 
})

这篇关于在 Firestore 文档中添加时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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