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...
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...
Share Improve this question edited Nov 20, 2020 at 3:08 koko-js478 2,03110 silver badges18 bronze badges asked Nov 20, 2020 at 3:02 diazAPdiazAP 251 silver badge8 bronze badges2 Answers
Reset to default 8This 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.
firebase.firestore().collection(collection).doc(doc).collection(collection)
.orderBy("time", "desc").limit(5)
This should get you the 5 newest documents by time. 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.
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);
}
});
});