如何更新 firebase firestore 中的文档 ID? [英] How to update the document ID in firebase firestore?

查看:27
本文介绍了如何更新 firebase firestore 中的文档 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 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 以便我可以在 id 内传递它code>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.

我建议您总体上进行更改,以解决您的问题,更重要的是为您提供稳固的结构.(来自 Auth 的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆