我可以在 Firestore 中发送创建文档的请求,但我想知道是否有一种方法可以将数组中的 n 个用户 ID/用户文档 ID 从 swift 或 typescript 中的 Cloud Functions 中添加到该文档中。
我知道我可以从数组中的用户集合中查询并获取所有用户,然后将 n 个随机用户添加到要在 Firestore 中创建的文档中,但如果我有 100 万用户,那么这将是一个百万阅读量,我正在努力避免这种情况。
代码
if let data = self.imageView.image?.pngData() {
let postID = APIs.posts.collection.document()
Services.storage.upload(path: "posts/\(postID).png", data: data, onSuccess: { (url) in
Services.crud.create(
ref: APIs.posts.collection.document(),
dict: [
"createdAt": Date(),
"fromUser": APIs.users.currentUserRef,
"post": url,
"timer": 5,
//"toUsers": [userID1, userID2, userID3, etc...] <- How I want to end up in Firestore
"type": "Image"
] as [String: Any],
onSuccess: {
self.openCamera()
}) { (error) in
print(error)
}
}) { (error) in
print(error)
}
}
最佳答案
如果您想知道集合中文档的所有 ID,您仍然需要查询整个集合才能获取所有 ID。 Firestore 中没有免费的“获取所有 ID”操作。没有查找随机选择的文档的操作。
关于Swift/Firestore/Cloud Functions - 向随机用户发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55038250/