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.
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.
Related Questions
- → I can't convert Json to string [OctoberCms]
- → Uncaught TypeError Illegal invocation when send FormData to ajax
- → Laravel Send URL with JSON
- → how to write react component to construct HTML DOM
- → AJAX folder path issue
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel - bindings file for repositories
- → Good solution to work with rest-api like SPA with redux?
- → getting the correct record in Angular with a json feed and passed data
- → Transformer usage on laravel/dingo API
- → Google options Page not saving - Javascript
- → Ember.js JSON API confusion
- → How can I query Firebase for an equalTo boolean parameter?