-2

I have excel file which contains some records. Requirement is I have to convert it into Mongodb database. I looking NodeJS solution. Is there any way to do it??

R. Richards
  • 24,603
  • 10
  • 64
  • 64

1 Answers1

0
'use strict';

const papa = require('papaparse');
const fs = require('fs');

//
// Read a text file form the file system.
//
function read (fileName) {
    return new Promise((resolve, reject) => {
        fs.readFile(fileName, 'utf8',
            function (err, textFileData) {
                if (err) {
                    reject(err);
                    return;
                }

                resolve(textFileData);
            }
        );
    });
};

//
// Helper function to import a CSV file.
//
function importCsvFile (filePath) {
    return read(filePath)
        .then(textFileData => {
            const result = papa.parse(textFileData, {
                header: true,
                dynamicTyping: true,
            });
            return result.data;
        });
};

function exportToMongoDB (db, collectionName, data) {
    return db[collectionName].insert(data);
};

const mongo = require('promised-mongo');

const db = mongo('localhost:27017/earthquakes', ['largest_earthquakes']);

importCsvFile('/code/data/earthquakes.csv')
    .then(data => exportToMongoDB(db, 'largest_earthquakes', data))
    .then(() => db.close())
    .catch(err => {
        console.error("An error occurred.");
        console.error(err.stack);
    });