简体   繁体   English

如何从 firestore 获取完整文档,包括填充的参考数据

[英]How to get Full Document from firestore including the reference Data populated

I am using Firestore in my Swift project.我在我的 Swift 项目中使用 Firestore。 I would like to get all the reference data inside the document populated when I fetch the list.我想在获取列表时获取填充文档中的所有参考数据。 But currently, I only get the reference Ids.但目前,我只得到参考 ID。

Here is the sample code that I am using to get the list of conversations这是我用来获取对话列表的示例代码

func getConversationByUserId(createdBy: String, completion: @escaping ([Conversation])->()) {
        let conversationRef = DBHelper.shared.database.collection("conversations")
        let createdByRef = DBHelper.shared.database.document("users/\(createdBy)")
        
        conversationRef
            .whereField("members", arrayContains: createdByRef)
            .addSnapshotListener() { (querySnapshot, err) in
                if let err = err {
                    print("Error getting documents: \(err)")
                } else {
                    if querySnapshot!.documents.isEmpty {
                        completion([])
                    } else {
                        var conversations: [Conversation] = []
                        if let snaps = querySnapshot?.documents {
                            for item in snaps {
                                conversations.append(Conversation(snapshot: item))
                            }
                        }
                        completion(conversations)
                    }
                }
            }
    }

样本数据

Firestore is a NoSQL database, we need to switch our coding mind from SQL database. Firestore 是一个 NoSQL 数据库,我们需要从 SQL 数据库切换我们的编码思路。

I would recommend to either store all related information in one document or you can fetch all needed information with multiple calls.我建议要么将所有相关信息存储在一个文档中,要么您可以通过多次调用获取所有需要的信息。

If you want to simplify the front-end logic, you can consider use Firebase Cloud Function to take over the multiple fetching logic.如果想简化前端逻辑,可以考虑使用 Firebase Cloud Function 来接管多次抓取逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Firestore 文档中获取嵌套参考数据? - How to get nested reference data at ones from Firestore document? 如何从 Firestore 获取对文档数组的引用 - How to Get Reference to Document Array from Firestore 如何从 Firestore 查询产生的快照文档中获取文档引用列表 - How to get the List of Document Reference from a Snapshot Document resulting from a Firestore query 如何将 GCP Firestore 的文档参考功能与 Spring Data FireStore 一起使用? - How to use Document Reference feature of GCP Firestore with Spring Data FireStore? 如何从 flutter 中的 firestore 文档字段获取数据? - How to get data from firestore document fields in flutter? 如何通过文档 ID 从 firestore 网站获取特定字段数据 - How get specific Field data by document id from firestore website 从 firestore 中的文档集合和文档子集合查询获取数据 - get data with query from document collection and document subcollection in firestore 如何从 Firestore 获取文档总数 - How to get Total document count from Firestore 如何从云功能中的 firestore 获取文档 - How to get a document from firestore in cloud functions 如何将 Firestore 文档引用存储为 nextjs 中的字段? - How do I store a Firestore document reference as a field from nextjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM