Reputation: 244
Given that I have x documents that all contain a key-value pair containing information on when the document was created (something like time:2021-05-06(timestamp)), how would I sort the documents in ascending order as quickly as possible?
I am using javascript and firebase firestore.
My code so far looks like this:
const docs = await getDocs(
collection(db, "users", auth.currentUser.uid, "valkompass")
);
// sort docs
Upvotes: 0
Views: 681
Reputation: 4109
As stated by @Frank , you can sort documents in ascending order by using the orderBy()
method. See sample code below:
const q = query(collection(db, "users"), orderBy("timestamp", "desc"));
const docs = await getDocs(q);
docs.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
For more information, you may check Order and limit data.
Upvotes: 1