wsc*_*rge 2 javascript node.js firebase google-cloud-firestore
我有以下文件的 firestore 集合:
[
{
start: { geohash: 'u3qchtmpuy2d' },
destination: { geohash: 'u3qcjvxfh9cs' },
timestamp: '28 June 2019 at 20:24:00 UTC+2',
...
}
]
Run Code Online (Sandbox Code Playgroud)
我试着像这样查询它(它的 Firestore Web SDK)
// 5-characters geohash is 4.89km × 4.89km
const start = 'u3qch';
const destination = 'u3qcj';
const timestamps = {
min: firestore.Timestamp.fromDate(
moment(someDateTime).subtract(10, 'minutes').toDate()
),
max: firestore.Timestamp.fromDate(
moment(someDateTime).add(10, 'minutes').toDate(),
),
};
firestore
.collection('deliveries')
.where('start.geohash', '>=', start)
.where('start.geohash', '<', geohashEnd(start))
.where('destination.geohash', '>=', destination)
.where('destination.geohash', '<', geohashEnd(destination))
.where('timestamp', '>=', timestamps.min)
.where('timestamp', '<', timestamps.max)
.get()
Run Code Online (Sandbox Code Playgroud)
的组合>=和<是从公司的FireStore:查询文件由startsWith一个字符串,因此geoHashEnd()函数(下这个问题的范围)。
它导致了以下错误:
FirebaseError:无效查询。具有不等式(<、<=、> 或 >=)的所有 where 过滤器必须位于同一字段中。但是你在 'start.geohash' 和 'destination.geohash' 上有不等式过滤器
我的问题:同时通过两个 geohash 字符串和一个附加字段查询我的 firestore 集合的最佳方法是什么?
根据有关查询限制的官方文档:
查询限制
Cloud Firestore 不支持以下类型的查询:
- 对不同字段使用范围过滤器的查询,如上一节所述。
如您所见,Cloud Firestore只能对单个字段执行范围过滤器,而不能按您的预期对多个字段执行范围过滤。最合理的解释是 Firestore 在这种情况下无法保证其性能。Firestore 必须能够在单个流中返回查询的所有结果。
要解决此问题,您必须查询数据库两次并在客户端合并这些查询的结果。这并不完美,因为您需要查询两次,但我认为它可以解决问题。
另请注意,在 geohashes 上使用范围过滤器进行查询不会返回非常准确的结果。为此,我建议您观看 Frank van Puffelen 关于此主题的精彩视频:
| 归档时间: |
|
| 查看次数: |
1237 次 |
| 最近记录: |