petey
petey

Reputation: 53

Import Multiple Entries to MongoDB from CSV file for Node.js App

First of all, I am new to Node.js and MongoDB. I have an idea for a web app, for work, that I am currently working on. It involves assigning data to reps in a database and then accessing that data within the app.

The data I have to use is in an Excel file. I am able to take that Excel file and create a CSV file from it to import into MongoDB. My issue is, I have multiple opportunities tied to individual reps. I am trying to figure out how to setup the database for my app in a way to where I can import this CSV file and it will save all the data for a rep to that particular rep in the database.

Here's an example of the data:
Rep Name,Opp ID,Opp Name,Prior Amount,Amount
Rep 1,1234561,Opp 1,10000,8000
Rep 1,1234562,Opp 2,15000,9000
Rep 2,1234563,Opp 3,20000,10000
Rep 1,1234564,Opp 4,25000,11000
Rep 2,1234565,Opp 5,30000,12000

Basically I'm wanting to be able to import the csv file, in this format, have a model within node.js that will support this import and then be able to pull the data for each rep within the app. For example, an overview of all the reps would show something like this: Rep 1 will have 3 Opps show up and Rep 2 will have 2 Opps show up

Is this even possible? Am I going about this the wrong way? Is there a better way of doing this? Any tips, tricks, suggestions or examples to help guide me would be great.

Upvotes: 4

Views: 9102

Answers (1)

mjhm
mjhm

Reputation: 16705

Since you seem to be familiar with ORM pattern I would recommend using the "mongoose" module. Though I would guess that you'll have a hefty learning curve with NodeJS and Mongo to make a solid app.

Here's a working example to get you started though:

#! /usr/bin/node

var mongoose = require('mongoose');
mongoose.connect('localhost', 'test');

var fs = require('fs');
var lineList = fs.readFileSync('mytest.csv').toString().split('\n');
lineList.shift(); // Shift the headings off the list of records.

var schemaKeyList = ['RepName', 'OppID', 'OppName', 'PriorAmount', 'Amount'];

var RepOppSchema = new mongoose.Schema({
    RepName: String,
    OppID: String,
    OppName: String,
    PriorAmount: Number,
    Amount: Number
});
var RepOppDoc = mongoose.model('RepOpp', RepOppSchema);

function queryAllEntries () {
    RepOppDoc.aggregate(
        {$group: {_id: '$RepName', oppArray: {$push: {
            OppID: '$OppID', 
            OppName: '$OppName',
            PriorAmount: '$PriorAmount',
            Amount: '$Amount'
            }}
        }}, function(err, qDocList) {
        console.log(util.inspect(qDocList, false, 10));
        process.exit(0);
    });
}

// Recursively go through list adding documents.
// (This will overload the stack when lots of entries
// are inserted.  In practice I make heavy use the NodeJS 
// "async" module to avoid such situations.)
function createDocRecurse (err) {
    if (err) {
        console.log(err);
        process.exit(1);
    }
    if (lineList.length) {
        var line = lineList.shift();
        var doc = new RepOppDoc();
        line.split(',').forEach(function (entry, i) {
            doc[schemaKeyList[i]] = entry;
        });
        doc.save(createDocRecurse);
    } else {
        // After the last entry query to show the result.
        queryAllEntries();
    }
}

createDocRecurse(null);

Your data in "mytest.csv":

Rep Name,Opp ID,Opp Name,Prior Amount,Amount
Rep 1,1234561,Opp 1,10000,8000
Rep 1,1234562,Opp 2,15000,9000
Rep 2,1234563,Opp 3,20000,10000
Rep 1,1234564,Opp 4,25000,11000
Rep 2,1234565,Opp 5,30000,12000

Upvotes: 7

Related Questions