[英]How to retrieve documents in firestore order with timestamp with React
我寫了下面的代碼。
const salons = query(collection(db, "user"), orderBy('', "desc"), limit(5))
const querySnapshot = await getDocs(salons)
await querySnapshot.forEach((doc) => {
console.log(new Date(doc._document.version.timestamp.seconds * 1000).toString())
//processing
})
我想按 console.log 中顯示的“doc._document.version.timestamp.seconds”對 orderby 進行排序。
但我不知道該怎么做。 這個日期是firestore自動注冊的,所以值的position是不一樣的。 有誰知道我該如何排序?
Firestore 只能根據它具有索引的值來排序/過濾數據。 索引僅為文檔中的字段(和文檔 ID)創建,而不是為隱式元數據創建,例如它在內部保留的時間戳。
無法根據內部時間戳對 Firestore 結果進行排序。 如果您希望能夠根據時間戳對文檔進行排序,則必須將該時間戳存儲為文檔中的一個字段,然后將該字段名稱傳遞給orderBy
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.