How to upload bulk data (json file) in Firestore subCollection

We have a Firebase project in which we have a collection (classes) and document("4EVIvXt9JvQs67WujZ1K7) and subCollection (subjects).

Database screenshot:

enter image description here

I want to upload "json" file data to my subcollection (subjects)

Here is my .js code, which i am using to upload data

const admin = require('./node_modules/firebase-admin');
const serviceAccount = require("./service-key.json");
const data = require("./datacsvjson.json");
const collectionKey = "classes"; //name of the collection
const document = "4EVIvXX9JvQs67WujZ1K";
const subCollection = "Science";

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://xxxxxxxxxxxxxxxxx.firebaseio.com"
});
const firestore = admin.firestore();
const settings = {
    timestampsInSnapshots: true
};
firestore.settings(settings);
if (data && (typeof data === "object")) {
    Object.keys(data).forEach(docKey => {
        firestore.collection(collectionKey).listDocuments(document).collection(subCollection).doc(docKey).set(data[docKey]).then((res) => {
            console.log("Document " + docKey + " successfully written!");
        }).catch((error) => {
            console.error("Error writing document: ", error);
        });
    });
}

But somehow this is updating my data in collection (classes) instead of subcollection (subjects).


Solution 1:

Thank you! I have got the solution to my question.

I used this code to import bulk data to Firestore database.

    npx -p node-firestore-import-export firestore-import -a C:\Users\myComputer\Downloads\serviceKey.json -b C:\Users\myComputer\Downloads\backup.json

Before executing this code, you should have installed node.js in your computer.

json file example would be like this.

 {
"__collections__": {

    "subjects": {
        "e0lGqxorYQngkTGP9K": {
            "className": "10th Class Math English",
            "__collections__": {
                "mat": {
                    "re152lyjhdA": {
                        "type": "video",
                        "index": "1",
                        "matName": "Video Lectures",
                        "matImage": "https://drive.google.com/u/0/uc?id=1TXSYYNOVwXUlZeL_u9Gbm2htsiZWKLoP&export=download",
                        "__collections__": {}
                    },

                    "MLoyOwPlOk7pMudhfjkB": {
                        "type": "pdf",
                        "index": "2",
                        "matName": "Detail Notes",
                        "matImage": "https://drive.google.com/u/0/uc?id=1_mZFJzyKqwXgm1P3Dx6toWph3wfvRyHq&export=download",
                        "__collections__": {}
                    },

                    "S5vu87Q2ywU49ie97": {
                        "type": "quiz",
                        "index": "3",
                        "matName": "Quiz",
                        "matImage": "https://drive.google.com/u/0/uc?id=1E2v95fOCtpZRjYlui5hyu4D4pk9tJB2J&export=download",
                        "__collections__": {}
                    },

                    "MRJ84bcjhdye6": {
                        "type": "pdf",
                        "index": "4",
                        "matName": "NCERT Book",
                        "matImage": "https://drive.google.com/u/0/uc?id=1gCikpM73irn29WP-I_hzKtCn2ra-gjDh&export=download",
                        "__collections__": {}
                    },

                    "sCYY46WByzSBI8jdy4": {
                        "type": "video",
                        "index": "5",
                        "matName": "NCERT Solution",
                        "matImage": "https://drive.google.com/u/0/uc?id=1ZwUuEzcmTtpwF4wQ0gGcxoFzCUKR2q0M&export=download",
                        "__collections__": {}
                    },

                    "PJlnt2Ttmjs64F": {
                        "index": "6",
                        "type": "pdf",
                        "matImage": "https://drive.google.com/u/0/uc?id=1suaNfLSuYD18W6Q_GAFs_UevIXaFspQn&export=download",
                        "matName": "Exemplar Book",
                        "__collections__": {}
                    },

                    "unefJcu6kz9qTJRId90mkiG": {
                        "matImage": "https://drive.google.com/u/0/uc?id=1MVp4b2fS4ahebJQ1_ULQc8bQumXb9RHi&export=download",
                        "type": "video",
                        "index": "7",
                        "matName": "Exemplar Solution",
                        "__collections__": {}
                    },

                    "bAmeNFhsy67vb5rd": {
                        "matImage": "https://drive.google.com/u/0/uc?id=1t-XjdiNapTEnCxDkYXZTJI7qEQwTtQch&export=download",
                        "matName": "Syllabus",
                        "index": "9",
                        "type": "",
                        "matUrl": "https://drive.google.com/drive/u/0/folders/1YtoAO95K8FU3sa1w82V9wJJstcC6WW0R",
                        "__collections__": {}
                    },
                    "qECitO71zwrinhu6b": {
                        "matImage": "https://drive.google.com/u/0/uc?id=1I449IerwdhlZF13609XTPt2-ygj-mNvE&export=download",
                        "type": "pdf",
                        "matName": "Sample Materials",
                        "index": "8",
                        "__collections__": {}
                    }

                }
            }
        }



    }

}

}

To validate your json file you can use https://jsonlint.com/

serviceKey - you can download from your Firebase.

backup.json - File which you want to upload.