【问题标题】:How to query a firestore array with multiple key value pairs in Swift如何在 Swift 中查询具有多个键值对的 Firestore 数组
【发布时间】:2021-09-14 02:55:02
【问题描述】:

我在 Cloud Firestore 中的一个集合有一个 where 数组中的每个项目都包含三个单独的值(请参阅 groupMembership 字段:

我知道我无法编写查询来查找与其中一个数组值匹配的文档。有没有办法编写查询来查找与所有三个项目的特定值匹配的文档?

例如,我想查找所有users,其中groupMembership 数组包含一个等于groupId: X, groupName: Y, membershipStatus: active 的对象

【问题讨论】:

    标签: swift firebase google-cloud-firestore


    【解决方案1】:

    您可以在array-contains clause 中传递整个对象。

    比如:

    usersRef
      .whereField("groupMembership", arrayContains: [
        "groupId": "kmDT8OUOTCxSMIBf9yZC",
        "groupName": "Jon and Charles Group",
        "membershipStatus": "pending",
      ])
    

    数组项必须完全且完全匹配您传入的字典。如果您只想过滤每个数组元素中的某些属性,则必须创建仅包含这些属性的附加字段。例如,使用(唯一的)组 ID 说字段 groupIds 是很常见的,因此您也可以过滤这些(当然在 limits of Firestore's queries 内)。

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 2018-03-26
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 2019-01-31
      • 2020-10-08
      相关资源
      最近更新 更多