How to Bulk Import a CSV File Using Multer With Node.js and MongoDB
Tags: mongodb, database, csv, node
Published: 2024-03-20 04:29:03

Explore my techLove blogs, which are ready to take you on an exciting voyage through the fascinating world of invention and technology with every click. Join me on a technology exploration of the latest trends and insights at https://codecraftsangita.blogspot.com/2024/03/how-to-bulk-import-csv-file-into.html
## **Introduction **

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:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/abaczncxq6p9hz05svcg.png)


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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnkj2cres1lq50r1yd5o.png)

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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym50rbkyetnor3g4q88n.png)

Create an index.js file in the root directory and include the code snippets listed below. This file sets up the web server.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqghtiglsmlym7am1q7e.png)

This file imports the Express and Express File Upload libraries, sets up the web application to use File Upload, and listens on port 3000. It also builds an Express route at "/" that will serve as the web application's default landing page. This route delivers an index.html file with a web form that allows users to upload CSV files.

To start your web server, type the following command in your terminal:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81vi3v7ostk2v94iq87b.png)

You should see the following message in your terminal: Server launched on port 3000. This signifies you've successfully connected to the web server.

In the root directory, create an index.html file. This file generates the form for uploading CSV files.

Let's look at the Author Schema. But, as you might expect, we need to install the Mongoose package first.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lu1yqo4q8lqo84qwz6hm.png)

## Create a Schema

Here, we will develop a student module by creating a student.js file that will store student data in the Student named collection.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hiilowkqmmhmaq23rihp.png)

 Create the Form Markup to Upload the Excel file in the index.ejs file

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5zvno7aiz5dx5iwtday.png)

## 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3uzrd7nzx2d83a0llu9.png)

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✌