Node js Upload CSV File to Mongodb Database Tutorial

In this tutorial, you will learn how to upload or import CSV file data in MongoDB in Node js + Express. 

How to Upload CSV File and Import in MongoDB using Node js + Express

Here are steps:

Step 1 – Create Node Express js App

Run the following command on cmd to create node js app:

mkdir my-app
cd my-app
npm init -y

Step 2 – Install express ejs body-parser mongoose CSVTOJSON Multer Modules

Execute the following command on the terminal to express ejs body-parser mongoose dependencies :

npm install -g express-generator
npx express --view=ejs

npm install csvtojson mongoose multer body-parser

Step 3 – Create Model

Create Models directory, and inside this directory, create studentModel.js file:

var mongoose  =  require('mongoose');

var csvSchema = new mongoose.Schema({
    FirstName:{
        type:String
    },
    LastName:{
        type:String
    },
    SSN:{
        type:String
    },
    Test1:{
        type:Number
    },
    Test2:{
        type:Number
    },
    Test3:{
        type:Number
    },
    Test4:{
        type:Number
    },
    Final:{
        type:Number
    },
    Grade:{
        type:String
    }
});

module.exports = mongoose.model('studenModel',csvSchema);

Step 4 – Create HTML Markup Form for Upload CSV

Create index.ejs file in the views directory, and create an HTML form in it to import CSV file:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>node js upload csv file to mongodb - tutsmake.com</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
</head>
<body>
        <nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
                <a class="navbar-brand" href="#">CsvToMongo</a>
              </nav>
    <div class="container">
        <div class=" nav justify-content-center" style="margin-top:100px;">
         <div class="card border-warning mb-3 " style="max-width: 20rem;">
                <div class="card-header"><h5>Upload csv file</h5></div>
                <div class="card-body">
                        <form action="/" method="post" enctype="multipart/form-data">
                            <input type="file" name="csv"><br><br>
                         <div class="text-center"><button type="submit" class="btn btn-lg btn-primary">submit</button></div>
                        </form>
                </div>
          </div>
    </div><br>
    <%if(data){%>
    <div>
        <table class="table table-hover table-responsive table-stripped  nav justify-content-center" style="width: auto" >
                <thead>
                    <tr class="bg-primary">
                        <th>S.no</th>
                        <th style="padding-right: 1em">LastName</th>
                        <th style="padding-right: 1em">FirstName</th>
                        <th style="padding-right:2em;padding-left:2em;">SSN</th>
                        <th>Test1</th>
                        <th>Test2</th>
                        <th>Test3</th>
                        <th>Test4</th>
                        <th>Final</th>
                        <th>Grade</th>
                    </tr>
                </thead>
                <tbody style="overflow-x: scroll; height:350px;" class="table-bordered">
                    <%for(var i=0;i< data.length;i++){%>
                    <tr class="text-center">
                                  <td ><%= i+1%></td>
                                  <td style="padding-right: 1em"><%= data[i].LastName%></td>
                                  <td style="padding-left: 1em;"><%= data[i].FirstName%></td>
                                  <td style="padding-right:1em;padding-left:1em;"><%= data[i].SSN%></td>
                                  <td style="padding-left: 1em"><%= data[i].Test1%></td>
                                  <td style="padding-left: 1em"><%= data[i].Test2%></td>
                                  <td style="padding-left: 1em"><%= data[i].Test3%></td>
                                  <td style="padding-left: 1.2em"><%= data[i].Test4%></td>
                                  <td style="padding-left: 1.2em"><%= data[i].Final%></td>
                                  <td style="padding-left: 1.2em"><%= data[i].Grade%></td>
                    </tr>
                    <%}%>
                </tbody>
        </table>
    </div>
    <%}%>
<br>
</body>
</html>  

Step 5 – Import Modules in App.js

Import express, body-parser, mongoose, multer dependencies in app.js; as shown below:

var express     = require('express');
var mongoose    = require('mongoose');
var multer      = require('multer');
var path        = require('path');
var csvModel    = require('./models/csv');
var csv         = require('csvtojson');
var bodyParser  = require('body-parser');

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});

//connect to db
mongoose.connect('mongodb://localhost:27017/csvdemos',{useNewUrlParser:true})
.then(()=>console.log('connected to db'))
.catch((err)=>console.log(err))

//init app
var app = express();

//set the template engine
app.set('view engine','ejs');

//fetch data from the request
app.use(bodyParser.urlencoded({extended:false}));

//static folder
app.use(express.static(path.resolve(__dirname,'public')));

//default pageload
app.get('/',(req,res)=>{
    csvModel.find((err,data)=>{
         if(err){
             console.log(err);
         }else{
              if(data!=''){
                  res.render('demo',{data:data});
              }else{
                  res.render('demo',{data:''});
              }
         }
    });
});

var temp ;

app.post('/',uploads.single('csv'),(req,res)=>{
 //convert csvfile to jsonArray
csv()
.fromFile(req.file.path)
.then((jsonObj)=>{
    console.log(jsonObj);
    //the jsonObj will contain all the data in JSONFormat.
    //but we want columns Test1,Test2,Test3,Test4,Final data as number .
    //becuase we set the dataType of these fields as Number in our mongoose.Schema().
    //here we put a for loop and change these column value in number from string using parseFloat().
    //here we use parseFloat() beause because these fields contain the float values.
    for(var x=0;x<jsonObj;x++){
         temp = parseFloat(jsonObj[x].Test1)
         jsonObj[x].Test1 = temp;
         temp = parseFloat(jsonObj[x].Test2)
         jsonObj[x].Test2 = temp;
         temp = parseFloat(jsonObj[x].Test3)
         jsonObj[x].Test3 = temp;
         temp = parseFloat(jsonObj[x].Test4)
         jsonObj[x].Test4 = temp;
         temp = parseFloat(jsonObj[x].Final)
         jsonObj[x].Final = temp;
     }
     //insertmany is used to save bulk data in database.
    //saving the data in collection(table)
     csvModel.insertMany(jsonObj,(err,data)=>{
            if(err){
                console.log(err);
            }else{
                res.redirect('/');
            }
     });
   });
});

//assign port
var port = process.env.PORT || 3000;
app.listen(port,()=>console.log('server run at port '+port));  

Step 6 – Test Application

You can use the following command to start node js app server:

//run the below command

npm start

Open browser and type the following URL in it:

http://127.0.0.1:3000/

Conclusion

That’s it, you have learned how to upload/import CSV file data into MongoDB in Node js + Express. 

Recommended Node JS Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Node js Upload CSV File to Mongodb Database Tutorial

  1. That’s really nice post. I appreciate your skills, Thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *