我希望能够从firebase中查询最新文档,并且发现我必须引用timestemp。
但是我的数据库中确实有自动生成的ID
我确实遇到了这个问题How to retrieve the last document in a firebase collection. I would also like to get the document fields value,发现您可以花一些时间来代替按键。
我像这样向Firebase添加和更新数据:
firebase.firestore().collection(collectionName).add({
data: data,
updated: getDate()
});
firebase.firestore().collection(collectionName).doc().update({
data: data,
updated: getDate()
});
答案 0 :(得分:1)
如果需要特定的字符串作为文档的ID,则不能使用add()
,也不能使用没有参数的doc()
。您将需要自己构建字符串,并将其传递给doc()
。
我个人不建议这样做。我建议接受随机ID,而应使用服务器时间戳作为文档中的字段。您可以使用它来对查询结果进行排序,并将结果集限制为1,以便获取最新的文档。