How to generate CSV using nodejs and mongodb with mongoose

Generate CSV using nodejs

In modern web applications you can save and export data in different formats like pdf or xml. Exporting data in web applications in CSV or Comma Separated Values format is a popular technique used by web developers now a days. In this tutorial you are going to learn how to generate CSV using nodejs and mongodb.

Following tasks are performed in this tutorial.

1. Install and run MongoDB on Windows, Mac or Linux system

2. Create a products database and a collection in MongoDB

3. Install NodeJS and express generator to generate application skeleton

4. Install csv-express and mongoose modules

5. Create a NodeJS application to generate CSV using NodeJS and mongodb.

generate csv using nodejs mongodb - prompts

Create products database and collection

To install mongodb, visit mongodb download page, download and install mongodb. You can read installation instructions on install page. In order to run mongodb, please visit this article and read section running mongodb.

After mongodb is setup, open command line and type mongod. Open another command line window and type mongo. MongoDB interactive shell starts. To create database productsdb, type following command.

use productsdb

If this database does not exist already, it is created otherwise current database will be changed to prodcutsdb. To create products collection and insert sample data, use insert command.

db.products.insert([{ "_id" : ObjectId("5899b8369ba2868af6d141d5"), "product_name" : "Apple IPhone 6", "price" : "$630", "category" : "Mobile Phone" } ,

{ "_id" : ObjectId("5899b8369ba2868af6d141d6"), "product_name" : "Sony Vio", "price" : "$1200", "category" : "Laptop" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141d7"), "product_name" : "Samsung T.V", "price" : "$900", "category" : "Electronics" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141d8"), "product_name" : "Apple IPAD", "price" : "$400", "category" : "Tablet" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141d9"), "product_name" : "MacBook Pro", "price" : "$800", "category" : "Laptop" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141da"), "product_name" : "Dell Laptop", "price" : "$620", "category" : "Laptop" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141db"), "product_name" : "Canon EOS 700D DSLR Camera", "price" : "$400", "category" : "Camera" }, 

... { "_id" : ObjectId("5899b8369ba2868af6d141dc"), "product_name" : "Nikon D7100 DSLR Camera ", "price" : "$440", "category" : "Camera" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141de"), "product_name" : "HTC Phone", "price" : "$200", "category" : "Mobile Phone" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141df"), "product_name" : "LG Monitor", "price" : "$500", "category" : "Electronics" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141e0"), "product_name" : "Samsung Printer", "price" : "$320", "category" : "Electronics" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141e1"), "product_name" : "Samsung Gear Live Black - Made for Android", "price" : "$250", "category" : "Watch" } ,

... { "_id" : ObjectId("5899b8369ba2868af6d141e2"), "product_name" : "Apple Watch", "price" : "$380", "category" : "Watch" } ])

To display inserted records, use find command.

db.products.find.pretty();

you can see all products as under.

csv using nodejs and mongodb

After database and collection is created, let us create an express application to generate csv using nodejs and mongodb.

Install NodeJS

Download NodeJS from download page of nodejs. After downloading, install NodeJS. Open command line and type following command to view latest version of NodeJS.

node -v

Install Express Generator

Express generator installs express tool. Express tool is used to generate NodeJS, express application. Express generator can be installed using NPM or Node package Manager.

npm install express-generator -g

After installation, Open directory where you want to create application. Like on Windows type cd c:\

Generate Express, NodeJS application

Using express tool, generate nodejs, express application. Pug is used as a template engine.

express --view=pug csv-using-nodejs-mongodb

This command generates csv-using-nodejs-mongodb application.

csv using nodejs mongodb - project generation

To install all dependencies and modules, run command.

cd csv-using-nodejs-mongodb && npm install

After modules installation, start nodejs server and run application.

SET DEBUG=csv-using-nodejs-mongodb:* & npm start

Now open browser and type. You can see application running.

http://localhost:3000

Install csv-express module

As product details are fetched from mongodb collection and are converted to a CSV file. For generating CSV, a NodeJS module csv-express is installed.

npm install csv-express --save

Install mongoose module

Mongoose is a nodejs module used as an ORM. We can interact with a mongodb database easily. It provides mapping to database collections.

npm install --save mongoose

Create a products Model to generate CSV using NodeJS

Open the project in your favorite IDE such as sublime text. Create a folder models. Create a file Product.js and add code below into it. This code defines a model for products collection in mongodb database. product_name, price and category fields are defined with String datatype. Finally to use product schema in other files, it is exported using module.exports.

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var productSchema = new Schema({

  product_name: { type: String, Required:  'Product name cannot be left blank.' },

  price:    { type: String,     Required:  'Product price cannot be left blank.'},

  category: { type: String ,    Required:  'Product category cannot be left blank'}

});

module.exports = mongoose.model('Products', productSchema);

Create Index route

Open index.js file in routes folder and replace the code below. In ‘/’ route, first csv-express and mongoose modules are included. In ‘/’ route, product schema’s find method is used to select records. {} is criteria to select records from database (Equivalent of  where clause in SQL).

Find method returns results in products variable in callback method. In response object’s render method title and products array is assigned to index view.

var express  = require('express');

var router   = express.Router();

var csv      = require('csv-express');

var mongoose = require('mongoose');

var Product  = mongoose.model('Products');

router.get('/', function(req, res, next) {

   Product.find({}, function(err, products) {

       if (err)

         res.send(err);

       res.render('index', { title: 'Nodejs MongoDB export to CSV', products: products });

   });

});

Create Index PUG template file

Add the code below in index.pug file in views folder. HTML table is created for id, product name, price and category fields. A loop through products array is performed and _id, product name, price and category of each product is displayed.

extends layout

block content
  .container
      .row
        .logo
          h3
           = title
      .row
        .span12
          .mini-layout
            h3 Products Listing
             a(href='/exporttocsv' style="margin-left: 60%;") Export to CSV

            table.table.table-bordered
              thead
                tr
                  th ID
                  th Prodcut
                  th Price
                  th Category
                if products.length
                each item in products
                 tr
                   td #{item['_id']}
                   td #{item['product_name']}
                   td #{item['price']}
                   td #{item['category']}

Add styles in CSS File

CSS Styles are added to css file in style sheets file in public directory. You can get the style sheet file from source code of the tutorial.

Display Product records

All products information is displayed with Export to CSV  link. Clicking on this link, products information is exported to a CSV file.

csv using nodejs mongodb all products

Generate CSV using NodeJs and MongoDB

To generate csv using NodeJS and mongodb, create a route exporttocsv in index.js file in routes directory.

Add exporttocsv route

Open index.js file in routes directory and add code. Inside callback method a CSV file name variable is defined. find method returns  mongodb collection object. lean method is used to convert mongodb documents to plain JavaScript array. In call back method products array is returned.

Response object’s status code is set to 200. Content-Type and Content Disposition properties with CSV file name are set using setHeader method. Next response object’s csv method exports records in a csv file.

 router.get('/exporttocsv', function(req, res, next) {

    var filename   = "products.csv";

    var dataArray;

    Product.find().lean().exec({}, function(err, products) {

        if (err) res.send(err);
        
        res.statusCode = 200;

        res.setHeader('Content-Type', 'text/csv');

        res.setHeader("Content-Disposition", 'attachment; filename='+filename);

        res.csv(products, true);

    });

 });

module.exports = router;

When user clicks on Export to CSV link, All records are fetched and user is prompted to save generated CSV file.

generate csv using nodejs mongodb - prompts

After user saves the file to computer. It can be opened to view records exported to CSV from database.

generate csv using nodejs mongodb

Summary

In this tutorial you learned about how to generate CSV using nodejs and mongodb. Products database and collection is created with sample data. We created an express, nodejs application. An exporttocsv route is created. When user clicks on exporttocsv link a csv file is generated.

Thanks you for reading this tutorial. You can download source code and database file from this link. Please leave your feedback and comments. To stay informed about upcoming articles follow us on twitter or subscribe to our newsletter.