【问题标题】:Cloud Firestore query Document with its collectionsCloud Firestore 查询文档及其集合
【发布时间】:2021-08-12 23:57:15
【问题描述】:

我在 Cloud Firestore 中有以下数据:

/任务

  • 身份证

  • 名称:字符串

  • 描述:字符串

  • assignees(每个任务内的assignees集合)

在查询我的任务时,我只得到名称、描述等基本数据。但不是任务中的受让人集合。 有没有办法通过一次读取来获取“整个”任务文档/对象?

我不想为每个任务执行“[taskId]/assignees”提取。

【问题讨论】:

标签: firebase firebase-realtime-database google-cloud-firestore nosql


【解决方案1】:

我不知道您的平台是什么,但访问子集合只是定义该集合的路径。

例如,如果您想访问受让人子集合,则可以这样定义路径(Swift,但跨平台的概念是相同的)

let tasksCollectionRef = self.db.collection("Tasks")
let taskDocRef = collectionRef.document("some task id")
let assigneesSubCollectionRef = userDoc.collection("assignees")

assigneesSubCollectionRef(completion: { documentSnapshot, error in
    //do somethingn with the documents within assignees

我不想为每个任务执行“[taskId]/assignees”提取。

因此,您可以使用这种代码流在代码中处理它

iterate over tasks
   read the assignees collection for each task

或者您可以更改模型并将受让人存储在每个任务中。您可以使用数组,甚至是地图来包含受让人

task_id          //document
   assignees     //map
      assignee_0 //map
         name    //field
      assignee_1 //map
         name    //field

当任务文档被读取时,被分配者将是一个可以读取的字段。在这里,我将assignees 字段读入字典数组并对其进行迭代

let assignees = doc.get("assignees") as! [String: Any]
for a in assignees {
   print(a.key, a.value)
}

【讨论】:

    猜你喜欢
    • 2021-01-08
    • 2020-07-26
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多