gpt4 book ai didi

swift - 创建 FireStore 数据结构

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:47 25 4
gpt4 key购买 nike

myPosts 是收藏。我想制作如下数据结构。

< Data Structure A >
myPosts(collection)
┣ userID1(document)
┃ ┣ postID1(collection)
┃ ┃ ┗ timestamp: 2018/09/21/22:22(field)
┃ ┣ postID2
┃ ┗ postID3
┣ userID2
┣ userID3

为了让数据结构如上,我写的代码如下

myPostsRef.document(userID).collection(postID).addDocument(data: [
timestamp_field: FieldValue.serverTimestamp()
]) { (error) in
if let _ = error { return }
}

然而,当这段代码被执行时,它具有以下结构。

< Data Structure B >
myPosts
┗ userID
┗ postID
┗ AutoGeneratedID
┗ timestamp: 2018/09/21/22:22

如何创建类似数据结构 A 的结构?

最佳答案

字段只能存在于文档中,而文档又只能存在于集合中。还建议使用固定的集合名称,因为您无法通过查询来获取子集合列表。所以我建议:

/myPosts (collection)
userId1 (document)
posts (collection)
post1 (document)
timestamp (field)

您可以通过以下方式获得:

myPostsRef.document(userID).collection("posts").document(postID).setData([
...

关于swift - 创建 FireStore 数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52436022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com