Ad

Filling Firestore Document With Data Programmatically

I already have some lists of data stored on my computer, and I want to upload them to my firestore database programmatically, without having to enter them one by one.

Have already seen some articles but none of them really worked for me.

***Note that I want to import the Initial Data that is not going to change over time, and the answer below is perfectly solving that. I have about 100K documents to import, so programmatical upload was very crucial.

Ad

Answer

For webapp NodeJS project you can do following steps:

1) Visit https://console.firebase.google.com/project/YOUR_PROJECT_ID/settings/serviceaccounts/adminsdk and download .json file which authorizes your Firebase Admin SDK.

2) Upgrade your Firestore to Blaze plan (pay as you go) as free version doesn't support importing data. You can manage/limit your spending here.

3) Paste the code you see in the page from step 1 to your .js file and use admin to import data like this:

admin
  .firestore()
  .collection("NAME_OF_YOUR_ORGANIZATION")
  .add({key: 'value', key2: 'value2'})

If you want to edit an existing document, you can do it like this:

admin
  .firestore()
  .collection("NAME_OF_YOUR_ORGANIZATION")
  .doc("UID_OF_EXISTING_DOCUMENT")
  .set({key: 'value', key2: 'value2'})

I suggest you to checkout the Admin SDK documentation.

Ad
source: stackoverflow.com
Ad