How to Import CSV File Records in MongoDB with Node Js
Node js upload CSV data to MongoDB tutorial; Throughout this guide, you will learn how to upload CSV file records to the MongoDB database using the Node js.
In order to import the CSV file to MongoDB, we will use some external dependencies such as Express, express-generator, multer, csvtojson, body-parser, and nodemon.
CSV stands for comma-separated values; it is used to keep the records line-wise. Every line represents a data record; every line contains one or more than one value.
To build such a feature, we will create an express route and make a mongoose schema that we will upload to the database using the multer module in the node js app.
Node Upload CSV Data in MongoDB using Node Js Example
- Step 1: Set Up Node App
- Step 2: Install NPM Dependencies
- Step 3: Build CSV Schema
- Step 4: Create CSV Import Form
- Step 5: Create Server File
- Step 6: Serve Node Application
Set Up Node App
Type and run the provided command to generate a new folder for keeping the project files.
mkdir node-parse-csv
After that move into the app folder.
cd node-parse-csv
A package.json file is required in the node project for installing and keeping records of the package that we use to build any feature and therefore execute the provided command.
npm init
Install NPM Dependencies
Here are the set of npm commands that you have to run from your command prompt to install all the packages simultaneously.
npx express --view=ejs
npm install -g express-generator
npm install multer csvtojson nodemon mongoose body-parser
Build CSV Schema
To build the schema for the csv file, make the models folder; there after, in this directory, make the EmpModel.js file.
Define the schema for name, email, designation, and mobile properties in the file.
var mongoose = require('mongoose');
var empSchema = new mongoose.Schema({
Name:{
type:String
},
Email:{
type:String
},
Designation:{
type:String
},
Mobile:{
type:Number
}
});
module.exports = mongoose.model('empModel', empSchema);
Create CSV Import Form
In order to import the data into the MongoDB database, we need to set up the view, create views/ directory, and make the index.ejs file, then insert the given code into the file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Node js Import CSV File to mongoDB Example</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
</head>
<body>
<form action="/" method="post" enctype="multipart/form-data">
<div class="mb-3">
<input type="file" class="form-control" name="csvFile" />
</div>
<button type="submit" class="btn btn-success">Upload</button>
<%if(data){%>
<div class="container mt-4">
<table class="table">
<thead>
<tr>
<th>#Id</th>
<th>Name</th>
<th>Email</th>
<th>Designation</th>
<th>Mobile</th>
</tr>
</thead>
<tbody>
<%for(var i=0; i < data.length; i++){%>
<tr>
<td><%= i+1%></td>
<td><%= data[i].Name%></td>
<td><%= data[i].Email%></td>
<td><%= data[i].Designation%></td>
<td><%= data[i].Mobile%></td>
</tr>
<%}%>
</tbody>
</table>
</div>
<%}%>
</form>
</body>
</html>
Create Server File
Get into the app.js file, this file is the pivot point of our node js, every nuances of this feature is being controlled from here.
In this file, we will set up the node js database connection, create routes using express js, handle CSV file upload using Multer, make a post request to upload the csv file, and enable the app connection.
var express = require('express')
var multer = require('multer')
var mongoose = require('mongoose')
var path = require('path')
var bodyParser = require('body-parser')
var csv = require('csvtojson')
var empSchema = require('./models/EmpModel')
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './public/uploads')
},
filename: (req, file, cb) => {
cb(null, file.originalname)
},
})
var uploads = multer({ storage: storage })
mongoose
.connect('mongodb://localhost:27017/demodb', { useNewUrlParser: true })
.then(() => console.log('Connected'))
.catch((err) => console.log(err))
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.resolve(__dirname, 'public')))
app.get('/', (req, res) => {
empSchema.find((err, data) => {
if (err) {
} else {
if (data != '') {
res.render('index', { data: data })
} else {
res.render('index', { data: '' })
}
}
})
})
var empResponse
app.post('/', uploads.single('csvFile'), (req, res) => {
csv()
.fromFile(req.file.path)
.then((response) => {
for (var x = 0; x < response; x++) {
empResponse = parseFloat(response[x].Name)
response[x].Name = empResponse
empResponse = parseFloat(response[x].Email)
response[x].Email = empResponse
empResponse = parseFloat(response[x].Designation)
response[x].Designation = empResponse
empResponse = parseFloat(response[x].Mobile)
response[x].Mobile = empResponse
}
empSchema.insertMany(response, (err, data) => {
if (err) {
console.log(err)
} else {
res.redirect('/')
}
})
})
})
var port = process.env.PORT || 5555
app.listen(port, () => console.log('App connected on: ' + port))
Serve Node Application
Our node app is ready to being served, hence run the provided command.
nodemon
You may check the application using the provided url:
http://127.0.0.1:5555
Conclusion
MongoDB is a popular tool for developers that helps build scalable applications. It is a NoSQL database that stores the data in document forms that are easy to manage with the JSON data format.
We are sure you have learned how to import CSV file data to MongoDB using the Node js framework, furthermore express js, multer, and most importantly, the csvtojson modules.