是否可以在Firestore中的同一查询中获取引用的文档?

问题描述 投票:0回答:1

我将以下数据添加到Firestore

Collection(posts)
 |--- postA
       |--- user: DocumentReference(users/userA)
       |--- text: Hello apple
 |--- postB
       |--- user: DocumentReference(users/userB)
       |--- text: Hello orange

Collection(users)
 |--- userA
      |--- name: AAA
 |--- userB
      |--- name: BBB

我打电话

Firestore.firestore().collection("posts").getDocuments { (querySnapshot, error) in
    // get posts array
    let data = querySnapshot?.documents.data()
    for post in data {
        // get user of each post
        let userRef = post["user"]
        userRef.getDocument { (document, error) in
            // finally get the user....
        }
    }
}

user文档不会直接返回post文档。只有DocumentReferenceuser。如果我需要每个userposts,它可能会引起太多的请求。

是否有可能在同一个查询中获取每个userpost文档?

firebase google-cloud-firestore
1个回答
1
投票

不,在获取文档时不会自动遵循文档引用。您必须为每个文档单独请求。

© www.soinside.com 2019 - 2024. All rights reserved.