简体   繁体   English

如何在 Swift 的 Firebase Firestore 中获取当前文档 ID?

[英]How to get current document id in Firebase Firestore in Swift?

I have these two document IDs (I am going to have more in the future)我有这两个文档 ID(我将来会有更多)

在此处输入图像描述

In some part of my code when the user taps on a Table View Cell, I am updating my Firebase using this code:在我的代码的某些部分中,当用户点击表格视图单元格时,我正在使用以下代码更新我的 Firebase:

let dbCollection = K.FStore.db.collection(K.FStore.User.UsersCollectionName).document("EMSce9qnfNnPmn8JBobn")
if let textFieldText = textField.text {
    dbCollection.updateData([
        self.userDataInsideTableViewKeysArray[indexPath.row] as! String : textFieldText
    ]){ err in
        if let err = err {
            print("Error updating document: \(err)")

        } else {
            print("Document successfully updated")
        }
    }
}

I have an app that when the user registers with the email and password authentication function provided by firebase, I create a db collection with that unique email alongside some other data (gender, age, phone number...) The document ID is randomly generated, I can get access to all documentsID if I loop between them, however I want the document ID of this specific one so I can update it, I want to replace document("EMSce9qnfNnPmn8JBobn") with document(documentID) for that I have multiple users I have an app that when the user registers with the email and password authentication function provided by firebase, I create a db collection with that unique email alongside some other data (gender, age, phone number...) The document ID is randomly generated ,如果我在它们之间循环,我可以访问所有documentID,但是我想要这个特定的文档ID,以便我可以更新它,我想用document(documentID)替换document(“EMSce9qnfNnPmn8JBobn”),因为我有多个用户

I'm not sure I understand correctly, but if you want to get the document for the currently logged in user, that'd be:我不确定我是否理解正确,但是如果您想获取当前登录用户的文档,那就是:

K.FStore.db.collection(K.FStore.User.UsersCollectionName).document(Auth.auth().currentUser.uid)

Also see the documentation on getting the currently signed-in user .另请参阅有关获取当前登录用户的文档。


Update更新

It seems that you have added your user profile documents with the addDocument calls, which means the IDs are unrelated to the user.您似乎已使用addDocument调用添加了用户配置文件,这意味着 ID 与用户无关。 In that case, you'll need to know something inside the document that uniquely identifies which document to update.在这种情况下,您需要知道文档中唯一标识要更新的文档的内容。

Since you say your email addresses are unique, and stored in the documents, you can use a query to find the matching document(s), and then loop and update each of them:由于您说您的 email 地址是唯一的,并且存储在文档中,因此您可以使用查询来查找匹配的文档,然后循环并更新它们中的每一个:

db.collection("cities").whereField("capital", isEqualTo: true)
    .getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                document.ref.updateData([
                    "fieldToUpdate": "newValue"
                ])
            }
        }
}

If you have a collection of user data, where each document belong to a specific user, and each user can only have a single document, it is idiomatic to use the UID as the document ID.如果您有一组用户数据,其中每个文档属于特定用户,并且每个用户只能拥有一个文档,则使用 UID 作为文档 ID 是惯用的。 To create a document with the UID as the document ID, see the first example in the documentation on setting a document .要使用 UID 作为文档 ID 创建文档,请参阅文档设置文档中的第一个示例。

If you use this data structure, you won't need a query to find the profile document for a user, and can instead use the statement at the top to determine what document to update.如果您使用此数据结构,您将不需要查询来查找用户的配置文件文档,而是可以使用顶部的语句来确定要更新的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM