Cloud Firestore : Is compound range query in more than 2 fields available now?

后端 未结 1 334
长情又很酷
长情又很酷 2021-01-20 21:28

I need to get ranged data like this.

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

a and b both fields are index key now.

I think in

1条回答
  •  别那么骄傲
    2021-01-20 22:00

    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.

    Option 1: Precompute a range

    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))

    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!

    Option 2: client-side filter

    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")

    0 讨论(0)
提交回复
热议问题