【问题标题】:How to query documents which contains a sent parameter in Firestore?如何在 Firestore 中查询包含已发送参数的文档?
【发布时间】:2018-03-23 02:22:29
【问题描述】:

我在 Android 中使用新的 Cloud Firestore。 所以我在firestore面临这个问题。 我想查询一个文档,它的数组字段中包含一个 id。

例如,我有一个文档,它在 firestore 中有一个字段,其中包含一个整数数组,如下所示:

所以当我有一个 id 66940 时,我可以获得包含该 id 的文档和其他文档,而不会返回不包含 id 66940 的其他文档。


除了这种方法,我还可以使用 id (66940) 在文档 id 中搜索它。我的文档 id 格式是这样的:
- 66940_88123
- 12323_66940

这个想法是,返回 ID 中包含 66940 的文档。 我读过一个解决方案here,但我不太明白如何实现它。

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    这是一个常见问题,因此我们发布了一些有关如何在 Cloud Firestore 中使用数组的文档: https://firebase.google.com/docs/firestore/solutions/arrays

    简而言之,如果您想查询数组成员资格,最好将数据存储为布尔映射。

    所以不要这样:

    {
      name: "sam",
      userGgIds: [12345, 67890]
    }
    

    你想要这个:

    {
      name: "sam",
      userGgIds: {
        12345: true,
        67890: true
      }
    }
    

    然后您可以执行此查询来搜索包含 ID“12345”的文档

    db.collection("users").where("userGgIds.12345", "==", true).get()
    

    【讨论】:

    • 啊当然!我希望我已经与文档间引用建立了联系。由于它们的重要性和共性,最好在文档中重申这将是成员关系的推荐用例(1:*、:)。是时候做一些旋转了:|
    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2018-12-09
    • 2021-06-10
    • 2010-11-16
    • 2020-06-18
    • 2023-04-05
    相关资源
    最近更新 更多