Home > Blockchain >  How to update a document id of a firestore database in Kotlin?
How to update a document id of a firestore database in Kotlin?

Time:11-21

Based on Firestore documentation, we can only update the field value. Is there a way to update the document id value? If there is, how to do that? I only found a way to update the field value which is like this:

    val washingtonRef = db.collection("cities").document("DC")

    washingtonRef
            .update("capital", true)
            .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully updated!") }
            .addOnFailureListener { e -> Log.w(TAG, "Error updating document", e) }

I cannot remove the .document("DC") and put .update() after db.collection("cities"), it seems like they have no .update() option after db.collection("cities"). Any help would be appreciated.

CodePudding user response:

Is there a way to update the document id value?

No, there is not. You cannot update a document ID or a collection name once they exist. All that data is immutable. If you need to change the ID of a document, then you should consider creating a new document with the correct document ID and deleting the old one.

  • Related