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

查看:77
本文介绍了在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天全站免登陆