【问题标题】:How to get all documents in a firestore subcollection based on the parent´s doc values?如何根据父文档值获取firestore子集合中的所有文档?
【发布时间】:2023-03-08 23:36:02
【问题描述】:

我在 Firebase Firestore 中有一个子集合的路径:Team/TeamName/Meetings/First/Data。我想知道是否有任何方法可以根据第一个文档(父文档)中的值(字段)获取数据子集合中的文档。

我试过了:

 db.collection("Team")
            .doc("TeamName")
            .collection('Meetings').where('0', '==', "some data")
            .where('3', '==', "doc data")
            .where('4', '==', "something in textfiel").collection("Data")

但是这种方法行不通。

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    Firestore 查询只能立即考虑查询的集合或子集合中的文档字段。没有“连接”可以跨多个集合组合文档之间的数据。

    您要执行的操作至少需要两个查询。首先,从“团队”获取父文档的查询:

    db
        .collection("Team")
        .where(/* your conditions on Teams documents here */)
        .get()
    

    然后,对于每个匹配的父文档,您将需要对每个子集合中的文档进行更多查询。

    db
        .collection("Team")
        .doc(matchingDocId)
        .collection("Meetings")
        .where(/* Any filters on documents in Meetings */)
        .get()
    

    【讨论】:

    • 团队是根(第一个)集合。
    • 我明白了,获取集合,然后通过与 doc.id 匹配的文档获取子集合。谢谢你的时间:)
    猜你喜欢
    • 2018-04-03
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多