【问题标题】:Flutter Firestore Query with arrayContainsAny : List is not working使用 arrayContainsAny 进行 Flutter Firestore 查询:列表不起作用
【发布时间】:2020-12-23 15:04:59
【问题描述】:

最新版本:Flutter + cloud_firestore: ^0.14.0+2

代码:

      FutureBuilder(
      future: _sessionsLogCollection
          .where('companyId', isEqualTo: sPData.companyId)
          .where('locationId', arrayContainsAny: [
            '29L73oSzdQrzLUow3Mg9',
            'bugdWVC6RRtHoemuxNWE',
          ])
          // .where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE')
          .orderBy('openDateTime', descending: true)
          .get(),

我已经创建了索引,所以这不是问题。

单独使用 .where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE') 时,查询返回正确的数据。

但单独使用 .where('locationId', arrayContainsAny: ['29L73oSzdQrzLUow3Mg9','bugdWVC6RRtHoemuxNWE']) 不会给出任何错误,而只是返回空数据集: sessionSnapShot.data.documents.length: 0.

** 使用 .whereIn 解决(可能 .whereIn 应该包含在 firebase.flutter.dev/docs/firestore/usage 中,因为它目前不存在)

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    根据您的代码.where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE'),似乎locationId 不是一个数组,而是一个字段。如果要查询数组,只能使用arrayContainsarrayContainsAny。但既然这是一个字段,那么你必须使用whereIn

              .where('locationId', whereIn: [
                '29L73oSzdQrzLUow3Mg9',
                'bugdWVC6RRtHoemuxNWE',
              ])
    

    使用 in 运算符可将同一字段上的最多 10 个相等 (==) 子句与逻辑 OR 组合在一起。 in 查询返回给定字段与任何比较值匹配的文档

    https://firebase.google.com/docs/firestore/query-data/queries#array_membership

    https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/query.dart#L393

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 2018-12-28
    • 1970-01-01
    • 2018-03-18
    • 2020-07-27
    • 2020-01-15
    • 2019-02-05
    相关资源
    最近更新 更多