【问题标题】:Flutter/Dart Firestore query: How do I get all docs where today falls between start/end dates in doc?Flutter/Dart Firestore 查询:如何获取今天在文档中开始/结束日期之间的所有文档?
【发布时间】:2021-08-04 04:17:45
【问题描述】:

我有一个查询,应该(应该)获取所有“DateLive”小于今天和“DateEnds”大于今天的文档。所以文档是“活的”,还没有“结束”。

这是我的 Dart 查询...

final todayAM = RepoUtil.todayTime000001();
final todayPM = RepoUtil.todayTime235959();
final querySnapshot = await _db
    .collection('things')
    .where('deleted', isEqualTo: false)
    .where('datelive', isLessThan: todayAM)
    .where('dateends', isGreaterThan: todayPM)
    .where('localzips', arrayContainsAny: localZips)
    .limit(100)
    .get();
final thingsJson = querySnapshot.docs.map((doc) => doc.data()).toList();
return thingsJson.map((thingJson) => Thing.fromJson(thingJson)).toList();

当我运行应用程序时,我得到了这个错误,我理解,但不知道如何解决......

I/flutter (1781): ThingView ERROR ThingListError([], 'package:cloud_firestore/src/query.dart': 断言失败: line 484 pos 18: 'hasInequality == field': All where filters不等式( 或 >=)必须在同一个字段上。但您在“FieldPath([datelive])”和“FieldPath([dateends])”上有不等式过滤器。).error

如何编写这个 Firestore 查询?

感谢您提供的任何帮助:-)

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    我认为这可能是 Firestore 的当前限制。它不会让您在同一查询中对 datelive 字段执行 isLessThan 并在 dateends 字段上执行 isGreaterThan

    您只能对单个字段执行范围(、>=)或不等于(!=)比较

    来源:https://firebase.google.com/docs/firestore/query-data/queries#compound_queries

    我认为您需要在 Firestore 中查询一个字段,然后在客户端过滤另一个字段。

    【讨论】:

    • 是的,我已经读过。不确定将大量文档加载到手机内存中然后进行过滤会有多好。我现在正在调查 Google Cloud Scheduler。看起来我可以创建一个计划函数,该函数将每天、半天或 w/e 运行,并根据文档中的两个日期更新单个 isLive 布尔字段。
    • 是的,同意,加载数据然后在客户端过滤它会很浪费。祝 Google Cloud Scheduler 好运,听起来是个不错的解决方案。
    • 感谢朋友的回复。我很感激:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多