Swift/Firestore/Cloud Functions - 向随机用户发送消息

标签 swift typescript google-cloud-firestore google-cloud-functions

我可以在 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/

相关文章:

javascript - 如何避免对此进行硬编码?在装饰器中

javascript - 类型错误 : Cannot read property 'value' of null in React Form

javascript - 火存储 : Query by item in array of document

swift - UserDefaults 检查 nil 值不起作用

ios - 如何使用 Swift 在 IOS 中使用自定义标题操作在 tableview 中显示 Collection View

swift - 扩展 UIView 时出现奇怪的动画

swift - 如何将字符串转换为 UIColor (Swift)

javascript - 按嵌套值过滤对象数组

javascript - 从函数结果中提取时间戳

javascript - 限制 firestore 中相等过滤器的数量?