我将以下数据添加到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
文档。只有DocumentReference
的user
。如果我需要每个user
的posts
,它可能会引起太多的请求。
是否有可能在同一个查询中获取每个user
的post
文档?
不,在获取文档时不会自动遵循文档引用。您必须为每个文档单独请求。