Firestore timestamp date range query with order by generate an error
我编写了一个查询来根据日期范围过滤一些数据,并希望按某个字段 (Int) 对结果进行排序。我编写的代码生成"FIRInvalidArgumentException"异常。
如果我删除了"order by",那么查询将完美执行。我已经为 visitDate 和 views 字段创建了索引。
索引:visitDate 升序视图降序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | let calendar = Calendar.current let currentDateComponents = DateComponents( calendar: calendar, year: calendar.component(.year, from: Date()), month: calendar.component(.month, from: Date()), day: calendar.component(.day, from: Date()), hour: 0, minute: 0) guard let currentDate = calendar.date(from: currentDateComponents) else { print("error"); return } guard let weekEndDate = calendar.date(byAdding: .day, value: -7, to: currentDate) else { print("error"); return } let queryRef = Firestore.firestore().collection("ArticleVisits") .whereField("visitDate", isGreaterThanOrEqualTo: weekEndDate) .whereField("visitDate", isLessThanOrEqualTo: currentDate) .order(by:"views", descending: true) .limit(to: limit) |
在日期范围内使用 order by 有什么问题吗?我需要为此创建一个特殊的索引吗?
感谢所有帮助。
@Jay 感谢您为我指明正确的方向。根据它说的文档,
However, if you have a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field:
所以我必须通过 visitDate 而不是 view 来订购它。