如何在Firebase Firestore中更新文档ID? [英] How to update the document ID in firebase firestore?
问题描述
在我的Firestore数据库中,我已将用户的电子邮件作为文档密钥.如果我想执行db.collection('users').doc('email_id')
来执行某些操作,该怎么办.现在的问题是,当用户更新其电子邮件ID时,我找不到任何方法来更新Firestore中的document id
.
in my firestore database, I've made the user's email as the document key. Do if I want I cal do db.collection('users').doc('email_id')
to perform some action. Now the problem is when the user is updating their email id, I am not finding any way to update the document id
in firestore.
我试图做
db.collection('users').doc(old_email).update({
id: new_email
})
但是实际上创建了一个名为id
的新字段,该新电子邮件作为该文档中的值,而不是更新实际的document id
,以便我可以在doc()
中传递它并获得与用户相同的数据.
But that actually created a new field called id
with the new email as value inside that document instead of updating the actual document id
so that I can pass it within doc()
and get the same data about the user.
有人知道怎么做吗?如果是这样,请分享.
Does anyone know how to do it? If so, please do share.
在发布此问题之前,我检查了google和firestore文档,但未找到任何更新文档ID的方法.请帮忙.
before posting this question I have checked google and firestore docs but didn't find any way to update the document id. Please help.
推荐答案
我不认为有一种更改文档ID的方法,但是即使有某种方法,您所做的事情也绝对是错误的.该ID应该是唯一标识符,让自己成为UserID字段,例如,使用Firebase Auth使用firebase.Auth().currentUser.uid
作为Firestore中用户特定的已保存数据的ID.
I dont think there is a way to Change the Document ID, but even if there is a way what you do is horribly wrong. The ID should be a Unique Identifier, make yourself a UserID Field, for example with Firebase Auth use firebase.Auth().currentUser.uid
as the ID of your User specific saved data in the Firestore.
我建议您总体上进行更改,以解决您的问题,更重要的是为您提供坚实的结构. (身份验证中的UID是唯一的)
I Suggest you to Change that in General that solves your Problem and more importantly gives you a solid structure. (The UID from the Auth is Unique)
这篇关于如何在Firebase Firestore中更新文档ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!