RandomSlav
RandomSlav

Reputation: 602

Best way to overcome Firestore compund queries limitations?

My document has fields distance and duration and I have to query by less than some distance (lets say 10km or 100km) and less than some duration (1h or 2h). Obviously when I tried to run a query I got the comparisons must all filter on the same field query error.

My solution was to calculate a boolean field for each of the distance and duration condition (distanceIsLessThan10Km, distanceIsLessThan100Km, durationIsLessThan1h, durationIsLessThan2h) which allows me to use queries on both distance and duration.

So now if user selects distance less than 10km and duration less than 2h I can do queries like

.Where("distanceIsLessThan10Km", distanceIsLessThan10Km, true)
.Where("distanceIsLessThan10Km", durationIsLessThan2h, true)

My question is is this a good solution or is there a better way to solve this?

Upvotes: 0

Views: 57

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599716

Turning your range conditions into equality checks is indeed a common workaround, and is feasible in cases where you have limited set of ranges to check for.

Since it seems you ranges are static there are very few downsides, but in some other cases you may need to update the additional fields - for which Cloud Functions are a great tool to use.

Upvotes: 1

Related Questions