簡體   English   中英

從 firestore 獲取最后 10 個文件

[英]Get the 10 last documents from firestore

我正在嘗試獲取在 Firestore 上創建的最后十個文檔。 我嘗試了一切,例如:

firebase.firestore()
    .collection("posts")
    .where("createdAt", "<=", new Date().getTime())
    .limit(10)

或者

firebase.firestore()
    .collection("posts")
    .orderBy("createdAt")
    .limit(10)

但不是返回最后 10 個,而是返回前 10 個。然后我嘗試了

firebase.firestore()
    .collection("posts")
    .limit(10)

它對前 11 個文件運行良好,但是當我通過該數量的文件時,它開始跳過一些文件。

首先,您需要為查詢定義排序順序。 如果您希望按該順序排列最后 10 個文檔,則應反轉排序順序。 如果您希望按createdAt字段對最后 10 個進行排序,則應定義如下排序順序:

firebase.firestore()
    .collection("posts")
    .orderBy("createdAt", "desc")
    .limit(10)

"desc" 將反轉默認排序順序,使它們從最大到最低排序。

我建議查看有關訂購和限制數據的文檔以了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM