![]() ![]() const mongodb = require("mongodb").MongoClient Remember that the code will be written inside one of these: Just save them to a MongoDB database collection. Now we have csvData that contains all neccesary rows. ![]() Import CSV data to MongoDB Database using mongodb – 'end' is emitted after the parsing is done (with all records), so we will save data (without the first item – header) to MongoDB in this. ![]() Rembemer that data is an array, so we translate it to an object. – 'data' is emitted when a record is parsed, so we will append that record ( data) in the handler function. In the code, there are 2 events: on('data') and on('end') that we implement the listener: save to the MongoDB database collection Let stream = fs.createReadStream("bezkoder.csv") The purpose for using fs is to create a ReadStream from CSV file, then use fast-csv module to parse the data and pass the result to ReadStream pipe: const fs = require("fs") It’s so simple.įirst, install fast-csv with the command: npm install fast-csv In the code above, fromFile() function returns a Promise that we receive the JSON-like array. const csvtojson = require("csvtojson") ĭescription: 'JavaScript runtime environment',ĭescription: 'JavaScript Framework for building UI',ĭescription: 'Platform for building mobile & desktop web app', Then we use csvtojson module to parse the data to JSON format from a CSV file. We will use fast-csv module, so run the command: npm install csvtojson Node.js import CSV data into MongoDB using fast-csv & mongodbĪssume that we have CSV file bezkoder.csv like this: id,name,description,createdAtġ,Node.js,JavaScript runtime environment,Ģ,Vue.js,JavaScript Framework for building UI,ģ,Angular.js,Platform for building mobile & desktop web app,Īll we need is to import each row (except header) to MongoDB colection category.Node.js import CSV data into MongoDB using csvtojson & mongodb.Import CSV data to MongoDB Database using mongodb. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |