简体   繁体   English

Firestore - 如何按时间升序获取 5 个最新文档

[英]Firestore - How to fetch 5 latest document with order by time asc

so I have the following code:所以我有以下代码:

    firebase.firestore().collection(collection).doc(doc).collection(collection)
       .orderBy("time", "asc").onSnapshot(function(querySnapshot) {
        querySnapshot.docChanges().forEach(function(change) {
            if (change.type === "added") {
                msgData(change);
            }
            if (change.type === "modified") {
                msgData(change);
            }
        });
    });

it lets me see a list of documents that are meant to be chat apps它让我看到了一个旨在成为聊天应用程序的文档列表

I will iterate the data that I retrieve in a message element, under the message element, there is an input textarea to add a new message.我将迭代我在消息元素中检索到的数据,在消息元素下,有一个输入文本区域来添加新消息。 The messages will be sorted from the smallest time to the largest time (timestamp) so that the newest message will appear right above the input textarea.消息将从最小时间到最大时间(时间戳)排序,以便最新消息将出现在输入文本区域的正上方。 Ilustration chat app插画聊天应用插画聊天应用

the problem is if the number of documents is large, the chat app will be very heavy because it load a large number of documents, so I want to limit messages to avoid this problem.问题是如果文档数量很多,聊天应用会很重,因为它加载了大量文档,所以我想限制消息以避免这个问题。 but whenever i find the solution is to sort time by descending and then limit.但是每当我发现解决方案是通过降序然后限制对时间进行排序。 if i do that, the newest chat message(message above input textarea) is the oldest data that i fetched...如果我这样做,最新的聊天消息(输入文本区域上方的消息)是我获取的最旧的数据...

This isn't possible with a single query without adding in some client code to sort the results a second time.如果不添加一些客户端代码来对结果进行第二次排序,这对于单个查询是不可能的。

You will want a descending sort with limit 5.您将需要限制为 5 的降序排序。

    firebase.firestore().collection(collection).doc(doc).collection(collection)
       .orderBy("time", "desc").limit(5)

This should get you the 5 newest documents by time.这应该会按时间为您提供 5 个最新的文档。 But then you will have to re-sort the documents by ascending time in thh client if you want to iterate or display them in ascending order.但是,如果您想以升序迭代或显示它们,则必须在客户端中按升序对文档进行重新排序。

The problem is solved.问题已经解决了。 all i have to do is to add reverse() at the end of docChanges function.我所要做的就是在 docChanges 函数的末尾添加 reverse() 。

firebase.firestore().collection(collection).doc(doc).collection(collection).orderBy("time", "desc").limit(10).onSnapshot(function(querySnapshot) {
    querySnapshot.docChanges().reverse().forEach(function(change) {
        var data = {
            id: change.doc.id,
            a: change.doc.data().a,
            b: change.doc.data().b,
            c: change.doc.data().c,
            d: change.doc.data().d,
            e: change.doc.data().e,
            f: change.doc.data().f,
        };

        if (change.type === "added") {
            msgData(data);
        }
        if (change.type === "modified") {
            msgData(data);
        }
    });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM