简体   繁体   English

如何在 Firebase/Google Cloud Firestore 的集合中获取最新添加的文档?

[英]How to fetch latest added document in a collection in Firebase / Google Cloud Firestore?

Using the web/JS platform, I want to retrieve the latest document added to a collection.使用 web/JS 平台,我想检索添加到集合中的最新文档。

  1. How do I accomplish this?我该如何实现?
  2. Do I need to append a timestamp when I save the data?保存数据时是否需要附加时间戳?
  3. Does the server automatically append a timestamp in the background on doc.add() ?服务器是否会在doc.add()的后台自动附加时间戳?
https://firebase.google.com/docs/firestore/query-data/get-data https://firebase.google.com/docs/firestore/query-data/get-data
 db .collection("cities") // .orderBy('added_at', 'desc') // fails // .orderBy('created_at', 'desc') // fails .limit(1) .get() .then(querySnapshot => { querySnapshot.forEach(doc => { console.log(doc.id, " => ", doc.data()); // console.log('timestamp: ', doc.timestamp()); // throws error (not a function) // console.log('timestamp: ', doc.get('created_at')); // undefined }); });

You could try using the onSnapshot method to listen to change events:您可以尝试使用onSnapshot方法来监听更改事件:

db.collection("cities").where("state", "==", "CA")
    .onSnapshot(function(snapshot) {
        snapshot.docChanges().forEach(function(change) {
            if (change.type === "added") {
                console.log("New city: ", change.doc.data());
                //do what you want here!
                //function for rearranging or sorting etc.
            }
            if (change.type === "modified") {
                console.log("Modified city: ", change.doc.data());
            }
            if (change.type === "removed") {
                console.log("Removed city: ", change.doc.data());
            }
        });
    });

Source: https://firebase.google.com/docs/firestore/query-data/listen来源: https : //firebase.google.com/docs/firestore/query-data/listen

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文档添加到云 Firestore 中的指定集合时如何更新文档? - How to update a document when a document is added to a specified collection in cloud firestore? 如何在 Firebase Firestore 中找到文档的父集合? - How to find the parent collection to a document in Firebase Firestore? Cloud Firestore 停止在用于 firebase 身份验证的集合中创建文档 - Cloud firestore stopped creating document in the collection used with firebase authentication Firestore - 如何按时间升序获取 5 个最新文档 - Firestore - How to fetch 5 latest document with order by time asc Cloud Functions:如何将 Firestore 集合复制到新文档? - Cloud Functions: How to copy Firestore Collection to a new document? 如何在 Firebase Cloud Firestore 中删除具有相同键值的集合中的文档 - How to delete documents in a collection with the same key value in Firebase Cloud Firestore Firebase Cloud Firestore,如何使用 javascript 仅接收 4 个文档? - Firebase Cloud Firestore, How to receive only 4 document using javascript? 如何使用 Firebase 云功能更新 Firestore 文档的值 - How to update value of a firestore document with a firebase cloud functions Firebase 云功能 - 将文档添加到 Firestore 时向 iOS 设备推送通知 - Firebase Cloud Function - Push a notification to iOS device when document is added to firestore 如何使用模块 Web SDK 的版本 9 在 Cloud Firestore 集合中的文档中获取文档和嵌套集合? - How do I get document and nested collection in a document in a Cloud Firestore collection using Version 9 of the Modular Web SDK?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM