在两个字段上同时使用firestore.where() [英] firestore.where() on two fields at once

查看:83
本文介绍了在两个字段上同时使用firestore.where()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件的存储库:

I have a firestore collection of the following documents:

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

并且我试图像这样查询它(它的Firestore Web SDK)

and I tried to query it like this (its 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:按以下方式查询文档用一个字符串开始,因此是geoHashEnd()函数(不在此问题的范围之内).

the combination of >= and < is from Firestore: query documents by startsWith a string, hence geoHashEnd() function (out of the scope of this question).

它导致了以下错误:

FirebaseError:无效的查询.不等式(<,< =,>或> =)的所有过滤器都必须位于同一字段上.但是您在'start.geohash'和'destination.geohash'上有不等式过滤器

FirebaseError: Invalid query. All where filters with an inequality (<, <=, >, or >=) must be on the same field. But you have inequality filters on 'start.geohash' and 'destination.geohash'

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

My question: what is the best approach to query my firestore collection by two geohash strings and an additional field, at once?

推荐答案

根据有关查询限制:

查询限制

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

Cloud Firestore does not support the following types of queries:

  • 如上一节所述,在不同字段上具有范围过滤器的查询.

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

So as you can see, Cloud Firestore can only do a range filter on a single field and not on multiple fields as you intended to do. The most reasonable explanation is that Firestore cannot guarantee its performance in this case. Firestore must be able to return all results of a query in a single stream.

要解决此问题,您将不得不两次查询数据库,并在客户端合并这些查询的结果.这不是完美的,因为您需要查询两次,但我认为它可以解决问题.

To solve this, you'll have to query the database twice and combine the results of those queries client side. It's not perfect, since you need to query twice but I think it will do the trick.

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

Please also note, that querying using range filters on geohashes will not return very accurate results. For that, I recommend you see Frank van Puffelen's remarkable video regarding this topic:

这篇关于在两个字段上同时使用firestore.where()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆