sis*_*438 1 javascript firebase google-cloud-firestore
使用 cloud-firestore 存储来自用户计算器的一些数据,而不是获取集合中的所有文档,我怎样才能获取最新的?
当前设置是:
db.collection("calculations")
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log("doc", doc);
document.getElementById("final-results").innerText +=
"\n" + doc.data().calcuation;
});
});
Run Code Online (Sandbox Code Playgroud)
Firestore 没有“最新文档”的内部概念。Firestore 应用于文档的唯一顺序是您使用添加到文档的字段定义的顺序。
如果您想要新近度的概念,则在将集合中的每个文档添加到集合中时,应使用服务器时间戳向集合中的每个文档添加一个时间戳类型字段,然后使用该字段查询该文档。然后您可以使用它来订购和限制文档。
如果您的文档中有一个名为“timestamp”的时间戳类型字段,这将为您提供最新的字段:
db.collection("calculations")
.orderBy("timestamp", "desc")
.limit(1)
.get()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2223 次 |
| 最近记录: |