Cloud Firestore:现在可以在两个以上的字段中进行复合范围查询吗? [英] Cloud Firestore : Is compound range query in more than 2 fields available now?

查看:85
本文介绍了Cloud Firestore:现在可以在两个以上的字段中进行复合范围查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得像这样的远程数据。

I need to get ranged data like this.

ex) 10 < a < 20 ,  30 < b < 40

a和b这两个字段现在都是索引键。

a and b both fields are index key now.

我认为在这种情况下,复合范围查询是一种简单的方法。

I think in this situation compound range query is the easy way.

但是在Cloud Firestore中,范围查询只能用于一个字段。

But in Cloud Firestore, range queries are possible only for one field.

ex)   (possible) citiesRef.where("state", "==", "CA").where("population", "<", 1000000)

    (impossible) citiesRef.where("state", ">=", "CA").where("population", ">", 100000)

如何在2个字段中进行范围查询?

How can I range query in 2 fields?

推荐答案

在撰写本文时,Cloud Firestore无法做到这一点,并且很可能永远无法使用。不过,根据您的要求,可以通过重新考虑数据模型和查询来解决问题。

As writing, this is not possible with Cloud Firestore and will likely never be available. Depending on your requirements though, there are workarounds by rethinking your data model and queries slightly.

在此选项中,可以添加一个名为 population_bracket 的字段。在这里,您实际上将总体值放在可以测试的方括号中。手动确定范围取决于您的需要,或者使用一些自动方法,例如 ceil(log(population))

In this option you can add a field called population_bracket. In here, you essentially place the population value into a bracket you can test. Either decide by hand what the ranges are depending on your need, or use some automated method like ceil(log(population))

例如,CA的人口大约为39250000,因此使用对数方法 ceil(log(population)),我们得到8,这是我们要存储在<$ c中的内容$ c> population_bracket 。如果我们可以将总体范围过滤器更改为< = 1000000 ,那么我们可以使用 population_bracket使总体过滤器成为简单的相等性检查== 6 。正确排除了CA!

As an example the population of CA is roughly 39250000, so using the log method ceil(log(population)) we get 8 which is what we'd store in population_bracket. If we're okay with the population range filter being changed to <= 1000000, then we can make the population filter a simple equality check using population_bracket == 6. CA gets correctly excluded!

在此选项中,您会做出有根据的猜测,即哪个范围过滤器会减少结果集,大多数情况下,只需对第二个过滤器的结果进行客户端过滤(例如,只需忽略每个状态< CA)

In this option you make an educated guess which range filter will reduce the resultset the most, then simply do client-side filter of the results for the second filter (e.g, just ignore every state < "CA")

这篇关于Cloud Firestore:现在可以在两个以上的字段中进行复合范围查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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