---
title: "Write a Node.js script to read data from a CSV file and store it in a MongoDB database."  
description: "Write a Node.js script to read data from a CSV file and store it in a MongoDB database."  
author: "Steilla Mitchel"  
published: 2023-07-26  
updated: 2023-07-27  
canonical: https://www.mindstick.com/forum/159298/write-a-node-js-script-to-read-data-from-a-csv-file-and-store-it-in-a-mongodb-database  
category: "node.js"  
tags: ["database", "node js", "mongodb"]  
reading_time: 2 minutes  

---

# Write a Node.js script to read data from a CSV file and store it in a MongoDB database.

Write a [Node.js](https://www.mindstick.com/articles/1499/upload-and-download-file-in-node-js) [script](https://www.mindstick.com/interview/477/how-can-i-execute-a-php-script-using-command-line) to read [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) from a [CSV file](https://www.mindstick.com/forum/393/how-to-import-csv-file-in-php) and [store](https://www.mindstick.com/articles/13125/tips-to-increase-sales-of-your-woocommerce-store) it in a [MongoDB](https://www.mindstick.com/articles/337565/understanding-document-stores-a-look-at-mongodb-vs-couchdb) database.

## Replies

### Reply by Aryan Kumar

Sure, here is a Node.js script to read data from a [CSV](https://www.mindstick.com/blog/257/write-data-in-excel-csv-and-xml-file-using-c-sharp) [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) and store it in a MongoDB database:

JavaScript

```plaintext
const csv = require("csv");
const mongodb = require("mongodb");

const client = new mongodb.MongoClient("mongodb://localhost:27017");

client.connect((err) => {
  if (err) {
    console.error(err);
    process.exit(1);
  }

  const db = client.db("mydb");
  const collection = db.collection("mycollection");

  const csvData = csv.parseFile("data.csv");

  csvData.forEach((row) => {
    const document = {
      name: row[0],
      age: row[1],
      address: row[2],
    };

    collection.insertOne(document, (err, result) => {
      if (err) {
        console.error(err);
      } else {
        console.log("Document inserted: ", result);
      }
    });
  });

  client.close();
});
```

This script first imports the `csv` and `mongodb` modules. Then, it creates a new MongoDB client and connects to the database.

Next, the script opens the CSV file and parses it into an array of objects. Each object in the array represents a row in the CSV file.

Finally, the script iterates through the array of objects and inserts each object into the MongoDB collection.

To run this script, you can save it as a file called `read_csv.js` and then run it from the command line:

```plaintext
node read_csv.js
```

This will read the data from the CSV file and store it in the MongoDB database. You can then query the database to view the data.


---

Original Source: https://www.mindstick.com/forum/159298/write-a-node-js-script-to-read-data-from-a-csv-file-and-store-it-in-a-mongodb-database

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
