我正在尝试获取在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个文档,它工作得很好,但当我传递了这么多文档时,它开始跳过一些文档。
发布于 2020-11-28 07:07:47
首先,您需要定义查询的排序顺序。如果您希望最后10个文档按该顺序排列,则应颠倒排序顺序。如果您希望最后10个按字段createdAt
排序,则应定义如下排序顺序:
firebase.firestore()
.collection("posts")
.orderBy("createdAt", "desc")
.limit(10)
"desc“将颠倒默认的排序顺序,使它们从最大到最低排序。
我建议查看documentation on ordering and limiting data以了解更多信息。
https://stackoverflow.com/questions/65044241
复制相似问题