Marton Zeisler
Marton Zeisler

Reputation: 199

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

I'm using real time Firebase database. Let's say I already have one million users and each user has fields like email, password, etc..

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.

I know I can do it manually in Firebase but it's not practical when we deal with a large and complex database

Upvotes: 3

Views: 9023

Answers (1)

Diego Venâncio
Diego Venâncio

Reputation: 6007

Why not create a method using Firebase .update running all documents?

Something like: (this is using 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. Post some code so we can see how to help you.

Upvotes: 3

Related Questions