Home > database >  Flutter firestore compound querying not working
Flutter firestore compound querying not working

Time:03-03

i have a compound firestore query. which dosent give any data but when called single it works this is the compound query

 final companyQuery = await _userCollection
          .collection("invoices")
          .where("company.id", isEqualTo: companyId)
          .where("date",
              isGreaterThanOrEqualTo: Timestamp.fromDate(dateTimeRange.start),
              isLessThanOrEqualTo: Timestamp.fromDate(dateTimeRange.end))
          .get();

but when i call like this it works

 final companyQuery = await _userCollection
          .collection("invoices")
   
          .where("date",
              isGreaterThanOrEqualTo: Timestamp.fromDate(dateTimeRange.start),
              isLessThanOrEqualTo: Timestamp.fromDate(dateTimeRange.end))
          .get();

also works like this too

inal companyQuery = await _userCollection
          .collection("invoices")
          .where("company.id", isEqualTo: companyId)
          .get();

my document screenshot firestore document

company objected expanded enter image description here

this is the print statement

companyId: lJHLM9sfcwXyPXo18iCX , dateTimeRange: 2022-01-30 00:00:00.000Z - 2022-03-02 21:25:13.563
[cloud_firestore/failed-precondition] The query requires an index. You can create it here:
https://console.firebase.google.com/v1/r/project/habllen/firestore/indexes?create_composite=Ckhwcm9qZWN0cy9oYWJsbGVuL2RhdGFiYXNlcy8oZGVmYXVsdCkvY29sbGVjdGlvbkdyb3Vwcy9pbnZvaWNlcy9pbmRleGVzL18QARoOCgpjb21wYW55LmlkEAEaCAoEZGF0ZRABGgwKCF9fbmFtZV9fEAE
error: FirestoreFailure.unexpected()

my index is

enter image description here

it dosent work when it queried together. please help me solve this issue. I have also tried adding composite index

CodePudding user response:

If you're missing a required index, the SDK will log an error saying so. This error includes a direct link to the Firebase console to create the required index, with all fields already populated. I recommend looking for such a message if you suspect the problem may be due to a missing index.

CodePudding user response:

I read firebase documentation and it looks like it is only possible to make compound queries on only one field. As this example from the documentation states:

cities_ref = db.collection("cities")
cities_ref.where("state", ">=", "CA").where("state", 
"<=","IN")

It won't work and even not give you an error message if you try to query with two different fields like this:

cities_ref = db.collection("cities")
cities_ref.where("state", ">=", "CA").where("population", 
">=", 1000000)

So, my suggestion to you will be, try to query the first part:

final companyQuery = await _userCollection
          .collection("invoices")
          .where("company.id", isEqualTo: companyId)
          .get();

Then, after getting your snapshots, do a conditional if-else statement on them to sort and get those you need. I hope it helps you out.

Check this reference: https://firebase.google.com/docs/firestore/query-data/queries

  • Related