Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Firestore中将数据存储到具有相同ID的多个集合?_Ios_Swift_Firebase_Google Cloud Firestore - Fatal编程技术网

Ios 如何在Firestore中将数据存储到具有相同ID的多个集合?

Ios 如何在Firestore中将数据存储到具有相同ID的多个集合?,ios,swift,firebase,google-cloud-firestore,Ios,Swift,Firebase,Google Cloud Firestore,我希望将数据存储在多个集合中,并应用Firebase Firestore的最佳实践。这是一份文件: [ "postID": documentID, "category": self.category, "title": self.postTitle, "description": self.postDescription, "date": self.postDate, "imageURL": self.post

我希望将数据存储在多个集合中,并应用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'
我该怎么做呢?

您可以将现有的三个DocumentReference对象一起使用,并将要添加到集合中的数据传递给字典。除了您已经在做的事情之外,您不需要对CollectionReference调用其他任何方法

let data = ... // your dictionary of data
allPosts.setData(data)

是的,那很好。但是对于其他引用,我想用日期嵌套文档,比如说,db.collection(“dates”).document(self.postDate).collection(documentID),这样每个日期都有多个文档。这是可以实现的,并且是正确的方法吗?正确的方法是满足您的查询。有很多种方法可以为数据建模,就像有很多种查询需要执行一样。如果模型不能满足您的查询,那么它不是一个很好的模型。