【问题标题】:Firestore Query For Non Existing Key of a MapFirestore 查询不存在的地图键
【发布时间】:2020-05-21 13:53:16
【问题描述】:

我知道可以对具有空值的字段执行空查询。

就我而言,我有包含 Map 的文档,我想查询所有这些不包含给定键的文档。在我的例子中,这个键是一个字符串格式的日期。

我确实尝试使用以下代码执行此操作,但它不起作用。相反,它获取所有文档

  Stream<List<Employee>> availableEmployeesForGivenDesignation(
      String designation, DateTime date) {
    // in firstore the keys of a map are Strings, thus I have to change DateTime to String
    var formatter = new DateFormat('yyyy-MM-dd');
    String formatedDate = formatter.format(date);
    //todo check why I am getting with null also the false employees
    return _employeeCollection
        .where('designations', arrayContains: designation)
        .where('busy_map.$formatedDate', isEqualTo: null) 
        .snapshots()
        .map((snapshot) {
      return snapshot.documents
          .map((doc) => Employee.fromEntity(EmployeeEntity.fromSnapshot(doc)))
          .toList();
    });
  }

据我了解,我查询的是地图键,对于某些文档,此键(日期)并不总是存在。这就是为什么我试图获取这些在这种情况下返回 null 的文档。

数据库结构:

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    找到答案了……

    我不得不将以下行更改为:

    .where('busy_map.$formatedDate', isNull: true)

    所以不要使用isEqual,而是使用isNull

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 2019-01-09
      • 2018-03-30
      • 2020-11-10
      • 1970-01-01
      • 2021-05-29
      • 2022-12-14
      • 2020-07-09
      相关资源
      最近更新 更多