问题描述
我正在使用 Google Firestore 地理查询 Following this documentation。使用 geohash 查询一定距离内的文档工作正常。当我引入一个新条件时:`.whereField("createdOn",isGreaterThan: 自 1970 年 7 天前的时间间隔值>)
这会引发错误,指出字段上的任何不等式都必须将此字段作为第一个“OrderBy”参数。当我为此字段添加 order by 参数时,它不再返回仍在搜索距离内但未显示错误的文档。
是否可以将 firestore geoqueries 与其他查询条件一起使用?
我需要能够通过在特定时间范围内创建的对象来限制查询,否则这将返回大量文档。对这些后查询进行排序肯定会影响应用程序的性能。也许我缺少在 Firestore 中使用地理查询的更实用的方法?
let queryBounds = GFUtils.queryBounds(forLocation: center,withRadius: distanceM)
//test
let ref = Ref().databaseJobs
let currentTime = NSDate().timeIntervalSince1970
let intervalLastWeek = currentTime - (10080 * 60)
print("current time is: \(currentTime) and 7 days ago interval was \(intervalLastWeek)")
ref.whereField("createdOn",isGreaterThan: intervalLastWeek)
let queries = queryBounds.compactMap { (any) -> Query? in
guard let bound = any as? GFGeoQueryBounds else { return nil }
return ref
.order(by: "geohash")
.start(at: [bound.startValue])
.end(at: [bound.endValue])
.whereField("createdOn",isGreaterThan: intervalLastWeek)
解决方法
Firestore 只能过滤单个字段的范围。或者更简单:您的查询中只能有一个 orderBy
子句。
您要执行的操作需要两个 orderBy
子句,一个用于 geohash
,另一个用于 createdOn
,这是不可能的。不过,如果您需要对第二个字段进行相等检查,那是可能的,因为 thstt 不需要 orderBy
子句。
我想知道您是否可以添加一个字段 createdOnDay
以固定格式只包含 createdOn
的一天部分,然后使用 7 对其执行 in
值(过去一周的几天?