
Import Excel file to Mongodb database
2 April, 2022
10
10
2
Contributors
MongoDB is an open-source NoSQL document-oriented database, that stores the data in JSON format. It is a distributed database that is designed to store a large scale of data.
In this article, we will look into how excel data can be imported to the MongoDB database and stored as a document in the specified collection. The implementation of MongoDB is done with Node.js + Express.js and it will read the data from the excel sheet and import it straight to your database.
Setting up the project
Create a project from the terminal
Install npm modules
Multer: Multer is a node.js middleware for handling multipart/form-data, primarily used for uploading files. https://vasantisuthar.hashnode.dev/import-excel-data-to-the-mongodb-database.
csvtojson : This module is used for converting CSV file to JSON file.
Create Node.js express app
Create a file named app.js and import the modules.
Student module in the database
Here, we are going to create a student module by creating a student.js file to store the student data in the Student named collection.
Create form markup to upload the excel file in index.ejs file
Here, the form will take input from an excel file, this file must be in CSV format.
Uploading the file to the database
Multer is quite popular for its efficiency and elementary for handling the files in an express server.
For the express app to accept the files, we will use Multer storage to handle the file uploads. This storage will be used to access the uploaded file.
App.js
The above code will create disk storage to store the file in the public folder of the root directory. By clicking on the submit button, data is fetched from each row of the uploaded file and will be imported to the student collection using insertMany method.
You can find the whole source code here Github
mongodb
web
node.js
multer