Export MongoDB Collection Data In CSV From Node Express Mongoose Application

Suppose a student collection in mongoDB have some data that we want to export in a CSV file from a Mongoose, Node, and Express app. For simplicity, let us assume only two fields in Mongoose schema: lastName and firstName. To export a CSV file, install moment, json2csv, and mongo-date-query (optional) npm packages:

npm install moment json2csv mongo-date-query --save

The required code is as follows:

'use strict';

const fs = require('fs');
const moment = require('moment');
const mdq = require('mongo-date-query');
const json2csv = require('json2csv').parse;
const path = require('path')
const fields = ['firstName', 'lastName'];
const Student = require('./student')

router.get('/', function (req, res) {
  Student.find({createdAt: mdq.lastYear()}, function (err, students) {
    if (err) {
      return res.status(500).json({ err });
    }
    else {
      let csv
      try {
        csv = json2csv(students, { fields });
      } catch (err) {
        return res.status(500).json({ err });
      }
      const dateTime = moment().format('YYYYMMDDhhmmss');
      const filePath = path.join(__dirname, "..", "public", "exports", "csv-" + dateTime + ".csv")
      fs.writeFile(filePath, csv, function (err) {
        if (err) {
          return res.json(err).status(500);
        }
        else {
          setTimeout(function () {
            fs.unlinkSync(filePath); // delete this file after 30 seconds
          }, 30000)
          return res.json("/exports/csv-" + dateTime + ".csv");
        }
      });

    }
  })
})

The breakdown of above code is:

  • Require fs to write file in export directory, moment to give a unique name to the file, json2csv to convert json to csv format, path to find exact path of directory for file temporary storage and Student Mongoose model

  • Define the fields we’re interested in exporting

  • Find students created last year

  • Convert student json response into csv with json2csv by passing fields as options

  • Define dateTime that includes date and time from year to seconds

  • Define file path of export

  • Write the csv file and respond the public link to it (The directory should be declared static in express server settings to be publicly available). The link will look like “/exports/csv-20180526052911.csv”, which the client (mobile/web app) can access and download by appending it to the base url of the server

  • Remove the file after a few seconds, as we’re not interested to keep the file in our directory indefinitely

Also check




See also

When you purchase through links on techighness.com, I may earn an affiliate commission.