【Firebase】 FireStore에 일괄 적으로 csv 또는 json 형식의 데이터 업로드
개요
FireStore 은 모바일 개발에 있어서 매우 편리한 Database의 하나입니다만, 데이터의 일괄 import가 조금 번거롭기 때문에 방법을 기재해 둡니다.
Step1: Json 데이터 준비
내 경우에는 Mac의 Numbers를 사용하여 CSV를 만들고 CSV→Json 변환 사이트을 사용하여 json 데이터로 변환하고 있습니다.


Step2: Service Key를 Firebase 콘솔에서 다운로드
Service Key를 Firebase 콘솔에서 다운로드합니다.


Step3: node.js 프로젝트 설정

위의 명령을 실행한 후 실행한 디렉토리에 Package.json이 생성됩니다.
Step1에서 생성한 json 파일(data.json) Step2에서 생성한 ServiceKey의 파일(serviceAccountKey.json)도 같은 디렉토리 내에 저장합니다.
Step4: index.js 만들기
terminal에서 다음을 실행합니다.
npm install firebase-admin
그러면 node_module이라는 디렉토리가 생성됩니다.

Firestore에서 import용 프로그램을 만듭니다.
index.js
const admin = require('./node_modules/firebase-admin');
const serviceAccount = require("./serviceAccountKey.json");
const data = require("./data.json");
const collectionKey = "words"; //Change!! name of the collection
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://your-database.firebaseio.com" //Change!! your database URL
});
const firestore = admin.firestore();
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);
if (data && (typeof data === "object")) {
Object.keys(data).forEach(docKey => {
firestore
.collection(collectionKey)
.doc(docKey)
.set(data[docKey])
.then((res) => {
console.log("Document " + docKey + " successfully written!");
}).catch((error) => {
console.error("Error writing document: ", error);
});
});
}
Step4: 실행
node <Your_Project_Folder_Name>
그러면
다음과 같이 대량으로 가져왔습니다.

기타
node의 버전이 낮으면 스프레드 구문이 대응하고 있지 않거나 등이 있으므로, 버전이 최신인지 확인해 주세요.
참고문헌
How to upload data to Firebase Firestore Cloud Database
Reference
이 문제에 관하여(【Firebase】 FireStore에 일괄 적으로 csv 또는 json 형식의 데이터 업로드), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/tetsukick/items/7912c0f49c742c63a8e6텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)