Ios 如何从Firebase Firestore获取特定的文档ID,其中ID是随机生成的,并且没有唯一标识符?

Ios 如何从Firebase Firestore获取特定的文档ID,其中ID是随机生成的,并且没有唯一标识符?,ios,swift,firebase,google-cloud-firestore,Ios,Swift,Firebase,Google Cloud Firestore,晚上,伙计们,我正在使用Firebase-Firestore进行最后一年的项目。在创建用户文档时,我使用了document(uid),但在我创建的请求文档中,一切都很顺利,如下所示 documentID是随机生成的,因为用户可以发出多个请求,所以documentID不能是相同的UID。现在,我陷入了一个用户收到请求并希望接受/拒绝的境地。我正在使用以下代码: let TrashAction = UIContextualAction(style: .normal, title: "D

晚上,伙计们,我正在使用
Firebase-Firestore
进行最后一年的项目。在创建用户文档时,我使用了
document(uid)
,但在我创建的请求文档中,一切都很顺利,如下所示

documentID
是随机生成的,因为用户可以发出多个请求,所以
documentID
不能是相同的
UID
。现在,我陷入了一个用户收到请求并希望接受/拒绝的境地。我正在使用以下代码:

let TrashAction = UIContextualAction(style: .normal, title:  "Decline", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
                
                let dbCollection = K.FStore.db.collection(K.FStore.RequestTransmission.collectionName).document("RDvjapleBC7TBBQ15NE9")
                    dbCollection.updateData([
                        K.FStore.RequestTransmission.isDeclined : true
                    ]){ err in
                        if let err = err {
                            print("Error updating document: \(err)")
                            
                        } else {
                            print("Document successfully updated")
                            DispatchQueue.main.async {
                                self.tableView.reloadData()
                            }
                        }
                    }
                
                success(true)
            })
            TrashAction.backgroundColor = .red
我想替换硬编码的
DocumentID
,这里:
K.FStore.db.collection(K.FStore.RequestTransmission.collectionName).document(“RDvjapleBC7TBBQ15NE9”)


怎么做?提前感谢

如果您需要获取文档,并且您知道文档中某个字段的值,则可以使用查询

// Create a reference to the cities collection
let collectionRef = db.collection("collectionName")

// Create a query against the collection.
let query = collectionRef.whereField("sender", isEqualTo: "test@domain.tld")
                .order(by: "timestamp", descending: true)
                .limit(to: 1)

query.getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                print("\(document.documentID) => \(document.data())")
            }
        }
它应该打印与查询匹配的所有文档的ID


您还可以使用
.limit(to:2)
来限制要返回的文档数量。

必须有一个唯一的标识符。。。。至少是发件人的电子邮件还是你可以使用的东西?@Dharmaraj这让我疯狂,用户可能有多个请求,如何选择什么文档?发件人是用户的电子邮件吗?是的,但用户(发件人)可以捐赠不止一次。我们来澄清一下,假设我的电子邮件是
test@domain.tld
,您需要发件人为
test@domain.tld
。我说的对吗?有办法得到最后一个吗?将其限制为1会返回最后一个吗?@mohamad我已经更新了答案,请尝试该查询。使用
.orderBy()
首先按时间戳降序排序,然后限制为1。