nodejs-export-mongodb-collection-csv-file-feature-image

Export MongoDB collection to CSV file using Node.js

In previous tutorial, we known the way to import CSV data to a MongoDB collection. Today, we’re gonna export MongoDB collection to CSV file using Node.js with two steps:

  • Read data from MongoDB collection using mongodb module
  • Export MongoDB data to CSV file using one of three modules: fast-csv, csv-writer or fs



Assume that our MongoDB database zkoder_db has a collection category like this:

db.category.find()

{ "_id" : ObjectId("5d91b4aaae94cf3414c5dd90"), "id" : "1", "name" : "Node.js", "description" : "JavaScript runtime environment", "createdAt" : "2019-09-03" }
{ "_id" : ObjectId("5d91b4aaae94cf3414c5dd91"), "id" : "2", "name" : "Vue.js", "description" : "JavaScript Framework for building UI", "createdAt" : "2019-09-06" }
{ "_id" : ObjectId("5d91b4aaae94cf3414c5dd92"), "id" : "3", "name" : "Angular.js", "description" : "Platform for building mobile & desktop web app", "createdAt" : "2019-09-09" }

What we will do is to get all rows and export them to bezkoder.csv file.

Read MongoDB collection

We use mongodb module to connect and get data from MongoDB database.

Run the command to install the module:

npm install mongodb

Now we can use it, the way looks like:

const mongodb = require("mongodb").MongoClient;

// let url = "mongodb://username:password@localhost:27017/";
let url = "mongodb://localhost:27017/";

mongodb.connect(
  url,
  { useNewUrlParser: true, useUnifiedTopology: true },
  (err, client) => {
    if (err) throw err;

    client
      .db("zkoder_db")
      .collection("category")
      .find({})
      .toArray((err, data) => {
        if (err) throw err;

        console.log(data);
        
        // TODO: write data to CSV file

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

What did we do? Look at the code above:
– First we import MongoClient class from mongodb module, it can be used for make a connection to MongoDB database.
– Next we initialize the url for the connection with host & port.
– Then we generate Collection object corresponding to category collection in zkoder_db database using client.db("zkoder_db").collection("category").
– We use find() method with {} parameter to tell that we want to query all documents.
– Finally we convert the finding result to array with toArray() function.

data parameter in the callback function will be:

[ { _id: 5d91bdbf77c1094324d00916,
    id: '1',
    name: 'Node.js',
    description: 'JavaScript runtime environment',
    createdAt: '2019-09-03' },
  { _id: 5d91bdbf77c1094324d00917,
    id: '2',
    name: 'Vue.js',
    description: 'JavaScript Framework for building UI',
    createdAt: '2019-09-06' },
  { _id: 5d91bdbf77c1094324d00918,
    id: '3',
    name: 'Angular.js',
    description: 'Platform for building mobile & desktop web app',
    createdAt: '2019-09-09' } ]

Export MongoDB data to CSV file using fast-csv

Run the command first: npm install fast-csv.
With this way, we need the help of fs module to create a WriteStream object.

Now import the module & use it:

const fastcsv = require("fast-csv");
const fs = require("fs");
const ws = fs.createWriteStream("bezkoder_mongodb_fastcsv.csv");

const data = ...;

fastcsv
  .write(data, { headers: true })
  .on("finish", function() {
    console.log("Write to bezkoder_mongodb_fastcsv.csv successfully!");
  })
  .pipe(ws);

Full code for this method will be like:

const mongodb = require("mongodb").MongoClient;
const fastcsv = require("fast-csv");
const fs = require("fs");
const ws = fs.createWriteStream("bezkoder_mongodb_fastcsv.csv");

// let url = "mongodb://username:password@localhost:27017/";
let url = "mongodb://localhost:27017/";

mongodb.connect(
  url,
  { useNewUrlParser: true, useUnifiedTopology: true },
  (err, client) => {
    if (err) throw err;

    client
      .db("zkoder_db")
      .collection("category")
      .find({})
      .toArray((err, data) => {
        if (err) throw err;

        console.log(data);
        fastcsv
          .write(data, { headers: true })
          .on("finish", function() {
            console.log("Write to bezkoder_mongodb_fastcsv.csv successfully!");
          })
          .pipe(ws);

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

Check bezkoder_mongodb_fastcsv.csv for result:

_id,id,name,description,createdAt
5d91bdbf77c1094324d00916,1,Node.js,JavaScript runtime environment,2019-09-03
5d91bdbf77c1094324d00917,2,Vue.js,JavaScript Framework for building UI,2019-09-06
5d91bdbf77c1094324d00918,3,Angular.js,Platform for building mobile & desktop web app,2019-09-09

Export MongoDB data to CSV file using fs

For this method, we need json2csv module. The module has Parser class that we can use parse() method to get the CSV formated data as a string. Then fs writeFile() function helps us to write the string to CSV file.

Install with the command: npm install json2csv.

const Json2csvParser = require("json2csv").Parser;
const fs = require("fs");

const data = ...;

const json2csvParser = new Json2csvParser({ header: true });
const csvData = json2csvParser.parse(data);

fs.writeFile("bezkoder_mongodb_fs.csv", csvData, function(error) {
  if (error) throw error;
  console.log("Write to bezkoder_mongodb_fs.csv successfully!");
});

The full code will be written like:

const mongodb = require("mongodb").MongoClient;
const Json2csvParser = require("json2csv").Parser;
const fs = require("fs");

// let url = "mongodb://username:password@localhost:27017/";
let url = "mongodb://localhost:27017/";

mongodb.connect(
  url,
  { useNewUrlParser: true, useUnifiedTopology: true },
  (err, client) => {
    if (err) throw err;

    client
      .db("zkoder_db")
      .collection("category")
      .find({})
      .toArray((err, data) => {
        if (err) throw err;

        console.log(data);
        const json2csvParser = new Json2csvParser({ header: true });
        const csvData = json2csvParser.parse(data);

        fs.writeFile("bezkoder_mongodb_fs.csv", csvData, function(error) {
          if (error) throw error;
          console.log("Write to bezkoder_mongodb_fs.csv successfully!");
        });

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

Export MongoDB data to CSV file using csv-writer

First run the command:
npm install csv-writer

Now we can import createObjectCsvWriter function from csv-writer module, the function needs 2 parameters (path & header) and returns a CsvWriter object.

const createCsvWriter = require("csv-writer").createObjectCsvWriter;

const data = ...;

const csvWriter = createCsvWriter({
  path: "bezkoder_mongodb_csvWriter.csv",
  header: [
    { id: "_id", title: "_id" },
    { id: "id", title: "id" },
    { id: "name", title: "name" },
    { id: "description", title: "description" },
    { id: "created_at", title: "created_at" }
  ]
});

csvWriter
  .writeRecords(data)
  .then(() =>
    console.log("Write to bezkoder_mongodb_csvWriter.csv successfully!")
  );

To write data to csv file, we use writer’s writeRecords() method.

Full implementation looks like:

const mongodb = require("mongodb").MongoClient;
const createCsvWriter = require("csv-writer").createObjectCsvWriter;

// let url = "mongodb://username:password@localhost:27017/";
let url = "mongodb://localhost:27017/";

mongodb.connect(
  url,
  { useNewUrlParser: true, useUnifiedTopology: true },
  (err, client) => {
    if (err) throw err;

    client
      .db("zkoder_db")
      .collection("category")
      .find({})
      .toArray((err, data) => {
        if (err) throw err;

        console.log(data);
        const csvWriter = createCsvWriter({
          path: "bezkoder_mongodb_csvWriter.csv",
          header: [
            { id: "_id", title: "_id" },
            { id: "id", title: "id" },
            { id: "name", title: "name" },
            { id: "description", title: "description" },
            { id: "created_at", title: "created_at" }
          ]
        });

        csvWriter
          .writeRecords(data)
          .then(() =>
            console.log("Write to bezkoder_mongodb_csvWriter.csv successfully!")
          );

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

Source Code

You can find the complete source code for this example on Github.

Conclusion

Let’s remind. We know how to read collection from MongoDB database using mongodb module.
Then export MongoDB collection to a CSV file in three ways using:

  • fast-csv
  • csv-writer
  • fs

You can find how to do the opposite in the previous tutorial:
Import CSV file into MongoDB collection using Node.js

Happy learning! See you again.

Further Reading

One thought to “Export MongoDB collection to CSV file using Node.js”

Leave a Reply

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