1

I have a firebase application on the App Store. In the users collection in firebase, I have a bunch of fields like first_name, last_name, etc. I want to add another field (age) in all the existing documents. How can I do that? If I don't update it, the application gives errors saying that the field doesn't exist in the document.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Gaurav Raj
  • 186
  • 1
  • 9

1 Answers1

1

There is nothing special about this:

  1. Read all documents.
  2. Loop over the results.
  3. Update each of them.

Since you tagged with Cloud Functions, in Node.js this would be something like:

const usersRef = db.collection('users');
const snapshot = await usersRef.get();
await Promise.all(snapshot.docs.map(doc => doc.ref.update({age: 42})));

This is based on these links from the Firebase documentation:

You might also want to look at:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807