Search code examples
javascriptnode.jsfirebasegoogle-cloud-firestore

Firestore error: Cannot use multiple conditional where clauses on different properties


I'm tring to use multiple "where" filters on Firestore query, My query is based on the above example.

My query:

let colRef = firebase.firestore().collectionGroup('volumes')
colRef = colRef.where('days', 'array-contains', day)
colRef = colRef.where('start_time', '<=', time)
colRef = colRef.where('end_time', '>', time)
colRef = colRef.where('volume', '>', 0)
const snapshot = await colRef.get()

I'm receiving the above error:

Error: 3 INVALID_ARGUMENT: Cannot have inequality filters on multiple properties

I have checked about compounding Firestore queries and I found out that:

You can perform range (<, <=, >, >=) or not equals (!=) comparisons only on a single field

It's a huge limitation to Firestore queries. Am I missing an another option to implement that query?

Thanks!


Solution

  • Update (April 2024): Firestore recently added the ability to have inequality and range condition on multiple fields in a single query. See the documentation on Query with range and inequality filters on multiple fields and Optimize queries with range and inequality filters on multiple fields for full details.


    Old answer below 👇

    Range condition in Firestore can only be applied on a single field. This is a hard limit, that is well documented (as you've already included in your question).

    Firestore only offers APIs where it can meet its performance guarantees, the main one of which is that queries return results in a time that is only related to the amount of data returned and not to the amount of data that needs to be searched for these results.

    If you need more flexible queries than Firestore offers, you should consider using another database. But you'll likely loose the Firestore performance guarantee with that other database.