简体   繁体   English

Flutter firestore 复合查询不工作

[英]Flutter firestore compound querying not working

i have a compound firestore query.我有一个复合 firestore 查询。 which dosent give any data but when called single it works this is the compound query which dosent 提供任何数据,但是当调用 single 它时它起作用这是复合查询

 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文件

company objected expanded公司反对扩大在此处输入图像描述

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我的索引是

在此处输入图像描述

it dosent work when it queried together.当它一起查询时它确实起作用。 please help me solve this issue.请帮我解决这个问题。 I have also tried adding composite index我也试过添加复合索引

If you're missing a required index, the SDK will log an error saying so.如果您缺少必需的索引,SDK 将记录一条错误信息。 This error includes a direct link to the Firebase console to create the required index, with all fields already populated.此错误包括指向 Firebase 控制台的直接链接以创建所需的索引,所有字段均已填充。 I recommend looking for such a message if you suspect the problem may be due to a missing index.如果您怀疑问题可能是由于缺少索引引起的,我建议您查找此类消息。

I read firebase documentation and it looks like it is only possible to make compound queries on only one field.我阅读了 firebase 文档,看起来只能在一个字段上进行复合查询。 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.然后,在获取快照后,对它们执行条件 if-else 语句以排序并获取所需的快照。 I hope it helps you out.我希望它能帮助你。

Check this reference: https://firebase.google.com/docs/firestore/query-data/queries检查此参考: https://firebase.google.com/docs/firestore/query-data/queries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM