gpt4 book ai didi

javascript - 在 firebase 文档中添加时间戳而不是自动生成的 key

转载 作者:行者123 更新时间:2023-12-02 21:16:13 24 4
gpt4 key购买 nike

我希望能够从 firebase 查询最新文档,并且我发现我必须引用 timestemp。

但是在我的数据库中我确实有自动生成的 ID

enter image description here

我确实遇到过这个问题How to retrieve the last document in a firebase collection. I would also like to get the document fields value并发现你可以用时间干代替 key 。

我像这样向 firebase 添加和更新数据:

 firebase.firestore().collection(collectionName).add({
data: data,
updated: getDate()
});

firebase.firestore().collection(collectionName).doc().update({
data: data,
updated: getDate()
});

所以我想知道你如何实现使用日期作为键
enter image description here

最佳答案

如果你需要一个特定的字符串作为文档的ID,那么你不能使用add(),也不能使用doc() 没有参数。您需要自己构建字符串,并将其传递给 doc()

就我个人而言,我不推荐这样做。我建议接受随机 ID,而使用服务器时间戳作为文档中的字段。您可以使用它对查询结果进行排序,并将结果集限制为 1 以获取最新文档。

关于javascript - 在 firebase 文档中添加时间戳而不是自动生成的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60962899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com