how to submit and store the csv file in mongodb?

20 views
Skip to first unread message

Dinesh Chebolu

unread,
Apr 20, 2018, 9:53:08 AM4/20/18
to nodejs

I am new to node.js, I am able to store the csv data in mongodb but my approach is when the user upload a file and hit the submit button it will automatically stored in mongodb. can any one suggest how to overcome the task?
please help me thanks



var fs= require('fs');
var csv = require('fast-csv');

var mongodb = require('mongodb');
var url = 'mongodb://localhost:27017/CSVDATA';
var MongoClient = mongodb.MongoClient;


var data;

MongoClient.connect(url, function (err, db) {
if (err) {
  console
.log('Unable to connect to the mongoDB server. Error:', err);
} else {
  console
.log('Connection established to', url);
 
var collection = db.collection('INFORMATION');
  readData
=fs.createReadStream('data.csv').pipe(csv())
               
.on('data',function(data){

                  collection
.insert({'data':data})

               
})
               
.on('end',function(data){
                  console
.log('Read finished');
               
})

}
});


Reply all
Reply to author
Forward
0 new messages