首页 > 解决方案 > OrderBy 时间戳 desc Firestore

问题描述

我目前正在使用firestore,我正在实施一项评论服务,客户可以在其中对课程进行评分和评论,我正在尝试按时间戳按降序检索评论,但快照是空的,但是当我按升序检索评论,快照带有结果。我过滤评论以匹配给定的课程,并确保评论不会因语言不好而被禁止。所以最后,我有一个这样的查询。

firestore
  .collection("reviews")
  .where("course", "==", this.course.id)
  .where("censored", "==", false)
  .orderBy("timestamp", "desc") 
  .startAfter(this.pagination)
  .limit(5)
  .get()

我正在查询的集合是评论集合,评论包含以下属性

{
 // If a review was censored
 censored: boolean,
 // The comment of the review
 comment: string,
 // The score of the review from O to 5
 score: number,
 // The course uid that is being reviewed
 course: string,
 // The timestamp when the review was created
 timestamp: timestamp, (time mark)
 // The user that review the course
 user: {
  email: string,
  name: string,
  id: string,
 }
}

我希望得到以降序排列的评论结果,以查看从最新到 oldesd 的评论。但是查询仅在以升序方式排序时才带有结果。查询中没有错误,仅带有empty: trueand size: 0

创建升序和降序索引。

上升指数 censored Ascendente course Ascendente timestamp Ascendente

降序索引 censored Ascendente course Ascendente timestamp Descendente

我已经通过更改.getfor a尝试了解决方案.onSnapshot,但我没有工作。

标签: javascriptfirebasegoogle-cloud-firestore

解决方案


推荐阅读