简体   繁体   English

Firebase-如何为现有数据库中的每一行添加一个新字段?

[英]Firebase - How do I add a new field for each row in an existing database?

I'm using real time Firebase database. 我正在使用实时Firebase数据库。 Let's say I already have one million users and each user has fields like email, password, etc.. 假设我已经有100万用户,每个用户都有电子邮件,密码等字段。

What if I suddenly want to add a new field to every user in my database? 如果我突然想向数据库中的每个用户添加一个新字段怎么办? Like I want each user to have a field called age. 就像我希望每个用户都有一个名为age的字段。

I know I can do it manually in Firebase but it's not practical when we deal with a large and complex database 我知道我可以在Firebase中手动完成此操作,但是当我们处理大型而复杂的数据库时,这是不切实际的

Why not create a method using Firebase .update running all documents? 为什么不使用运行所有文档的Firebase .update创建方法?

Something like: (this is using Firebase firestore) 类似于:(这是使用Firebase Firestore)

updateUser() {
        this.db.collection('yourDbCollection').doc('ifYourIdCostumized').update({
            age: newAgeHere
        })
            .then(function () {
                console.log("Document successfully updated!");
            }).catch(function (error) {
                console.error("Error removing document: ", error);

            });
    }

Or you can use Firebase functions if you want to make it server-side. 或者,如果您想使其成为服务器端,则可以使用Firebase函数 Post some code so we can see how to help you. 发布一些代码,以便我们了解如何为您提供帮助。

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

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