LostTexan
LostTexan

Reputation: 891

How many .where clauses can I use in firestore query in a flutter app?

Is it possible to use these .where clauses in a firestore query?

Stream<List<Trxns>> _streamFromTrxns = _db.collection('agency').doc(globals.agencyId)
        .collection('trxns')
        .where('trxnStatus', isNotEqualTo: 'Closed')
        .where('contractDate', isGreaterThanOrEqualTo: kFirstDay)
        .where('closingDate', isLessThanOrEqualTo: kLastDay)
        .snapshots().map((snapshot) => snapshot.docs
        .map((document) => Trxns.fromFirestore(document.data()))
        .toList());

I am getting an error because each .where needs to be associated with the same field. How do I implement the filters I have above only in a firestore query?

Upvotes: 0

Views: 59

Answers (1)

LeadDreamer
LeadDreamer

Reputation: 3499

You don't, as expressed. There's a (scaling) reason Firestore only allows inequality tests on a single field - noting that != is actually an inequality. (https://firebase.google.com/docs/firestore/query-data/queries)

You will need to find another way to structure your data, OR you need to decide if one of the inequalities reduces the number of results enough such you can filter the other two on the client. Looking at your example, you might consider a different set of values for 'trxnStatus' - ```'=='`` is NOT considered an inequality, so it would allow at least one of the other inequalities (think some encoding of "not-closed" as a value).

You can have more .where() queries - but they can't be inequalities unless they are on the same field.

Upvotes: 2

Related Questions