我有数据要存储在多个集合中并应用 Firebase Firestore 的最佳实践。这是一个文档:
[
"postID": documentID,
"category": self.category,
"title": self.postTitle,
"description": self.postDescription,
"date": self.postDate,
"imageURL": self.postImageURL
]
我的想法是通过相同的 ID 在多个位置存储相同的文档,以便更快地检索。这是我正在尝试做的事情:
let documentID: String = db.collection("collectionName").document().documentID
let allPosts: DocumentReference = db.collection("allPosts").document(documentID)
let postsByDate: DocumentReference = db.collection("dates").document(self.postDate).collection(documentID)
let postsByCategory: DocumentReference = db.collection("category").document.(self.category).collection(documentID)
当我一劳永逸地生成 documentID 时,我不想 addDocument
而是 setData
这样它就不会用另一个 ID 嵌套数据,而是用 CollectionReference似乎没有 setData
。
我什至尝试使用 db.document("dates").setData()
但会引发错误:
Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Invalid document reference. Document references must have an even number of segments, but dates has 1'
我该怎么办?
最佳答案
您可以使用 setData()使用您现有的三个 DocumentReference 对象,并将包含要添加到集合中的数据的字典传递给它。除了您已经在执行的操作之外,您不需要调用 CollectionReference 上的其他任何方法。
let data = ... // your dictionary of data
allPosts.setData(data)
关于ios - 如何将数据存储到 Firestore 中具有相同 ID 的多个集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52138686/