Import Excel Data to the MongoDB Database Using Multer
MongoDB stands out as a versatile and scalable NoSQL database management solution, providing an intuitive object modeling interface for Node.js.
As data volume increases, the necessity for effective data input procedures becomes critical. Uploading CSV files is a common format for storing tabular data. In this blog, we'll look at how to bulk import CSV files into MongoDB using Node.js.
Lets Set up the Project
To begin, create a new Node.js application. In a command prompt, browse to the location where you want to put your Node.js apps and run the following commands:
Using the -y lets the project to be initialised using the default values, resulting in the application starting with index.js. In your command prompt, enter the following command to install Express:
Because our application will accept files using a web form, we will also use the Express sub-package Express File Upload. Let's install it as well:
Create an index.js file in the root directory and include the code snippets listed below. This file sets up the web server.
To start your web server, type the following command in your terminal:
In the root directory, create an index.html file. This file generates the form for uploading CSV files.
Create a Schema
Create the Form Markup to Upload the Excel file in the index.ejs file
Uploading the File into the Database
Multer is well-known for its simplicity and effectiveness in file management on an express server.
To allow the express app to accept files, we will use Multer storage for file uploads. This storage will be used to retrieve the uploaded file.
The code above will generate disk storage for the file in the root directory's public folder. By hitting the submit button, data is retrieved from each row of the uploaded file and imported into the student collection via the insert many methods.
Happy Coding✌
Stay connected about the newest coding tips and tutorials by following Codegirl. Writing technical content isn't just a hobby for me—it's a continuous journey of learning and sharing with all of you.