Home > OS >  Is there a way to add new field to all of the documents in a firestore collection?
Is there a way to add new field to all of the documents in a firestore collection?

Time:04-12

I have a collection that needs to be updated. There's a need to add new field and fill it out based on the existing field.

Let's say I have a collection called documents:

documents/{documentId}: {
   existingField: ['foo', 'bar'],
   myNewField ['foo', 'bar']  
}

documents/{anotherDocumentId}: {
   existingField: ['baz'],
   myNewField ['baz']  
}
// ... and so on

I already tried to fire up local cloud function from emulator that loops for each document and writes to production data based on the logic I need. The problem is that function can only live up to max of 30 seconds. What I need would be some kind of console tool that I can run as admin (using service-account) to quickly manage my needs.

How do you handle such cases?

CodePudding user response:

Firebase does not provide a console or tool to do migrations.

You can write a program to run on your development machine that uses the one of the backend SDKs (like the Firebase Admin SDK) to query, iterate, and update the documents and let it run as long as you want.

CodePudding user response:

There is nothing specific built into the API for this type of data migration. You'll have to update each document in turn, which typically involves also reading all documents (or at least their IDs).

While it is possible to do this on Cloud Functions, I find it easier to do it with a local Node.js script, as that doesn't have the runtime limits Cloud Functions imposes.

  • Related