admin管理员组文章数量:1406945
using cloud-firestore to store some data from a user calculator, rather than getting all the documents in the collection, how can I just get the most recent?
current set up is:
db.collection("calculations")
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log("doc", doc);
document.getElementById("final-results").innerText +=
"\n" + doc.data().calcuation;
});
});
image of database here
using cloud-firestore to store some data from a user calculator, rather than getting all the documents in the collection, how can I just get the most recent?
current set up is:
db.collection("calculations")
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log("doc", doc);
document.getElementById("final-results").innerText +=
"\n" + doc.data().calcuation;
});
});
image of database here
Share Improve this question asked Jan 9, 2020 at 20:31 sisternight438sisternight438 1531 gold badge3 silver badges14 bronze badges 1- Here is a great tutorial: link – radulle Commented Jan 9, 2020 at 23:42
1 Answer
Reset to default 8Firestore doesn't have an internal concept of "most recent document". The only ordering that Firestore applies to documents are the ones that you define using the fields you add to the document.
If you want a concept of recency, you should include a timestamp type field using a server timestamp to each document in the collection when you add it to the collection, then query that document using that field. Then you can use that to order and limit documents.
If you have a timestamp type field called "timestamp" in your documents, this will give you the most recent one:
db.collection("calculations")
.orderBy("timestamp", "desc")
.limit(1)
.get()
本文标签: javascripthow do i retrieve the most recent document added to a collectionStack Overflow
版权声明:本文标题:javascript - how do i retrieve the most recent document added to a collection? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744680578a2619381.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论