首页 > 解决方案 > firestore.where() 同时在两个字段上

问题描述

我有以下文件的 Firestore 集合:

[
  {
    start:       { geohash: 'u3qchtmpuy2d' },
    destination: { geohash: 'u3qcjvxfh9cs' },
    timestamp:   '28 June 2019 at 20:24:00 UTC+2',
    ...
  }
]

我试着像这样查询它(它的 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()

>=和的组合<来自Firestore:通过 startsWith a string 查询文档,因此是geoHashEnd()函数(超出此问题的范围)。

它导致了以下错误:

FirebaseError:无效的查询。具有不等式(<、<=、> 或 >=)的所有 where 过滤器必须位于同一字段中。但是您在“start.geohash”和“destination.geohash”上有不等式过滤器

我的问题:一次通过两个 geohash 字符串和一个附加字段查询我的 firestore 集合的最佳方法是什么?

标签: javascriptnode.jsfirebasegoogle-cloud-firestore

解决方案


根据有关查询限制的官方文档:

查询限制

Cloud Firestore 不支持以下类型的查询:

  • 如上一节所述,在不同字段上使用范围过滤器进行查询。

如您所见,Cloud Firestore只能对单个字段进行范围过滤,而不能像您预期的那样对多个字段进行过滤。最合理的解释是 Firestore 在这种情况下无法保证其性能。Firestore 必须能够在单个流中返回查询的所有结果。

为了解决这个问题,您必须查询数据库两次并结合客户端查询的结果。这并不完美,因为您需要查询两次,但我认为它可以解决问题。

另请注意,在 geohashes 上使用范围过滤器进行查询不会返回非常准确的结果。为此,我建议您观看 Frank van Puffelen 关于该主题的精彩视频:


推荐阅读