Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to populate my MongoDB using data from a CSV file. There are currently no databases or collections in my MongoDB and I would like to create these using an update function that creates objects parsed from a csv file.

I am using ya-csv to parse my csv file and the mongodb driver for node.

My code looks like this:

var csv = require('ya-csv');
var fs = require('fs');
var MongoClient = require('mongodb').MongoClient;
var Server = require('mongodb').Server;
var mongoclient = new MongoClient(new Server('localhost', 27017, {'native_parser' : true}));

var reader = csv.createCsvFileReader('YT5.csv', {columnsFromHeader:true,'separator':   ','});
reader.addListener('data', function(data){
var nameHolder = data.name;
//I have no issue getting the needed variables from my csv file
mongoclient.db(nameHolder).collection('assets').update({assetId:data.assetId,name:data.name},{upsert:true},function(err,updated){if(err){console.log(err)}});

reader.addListener('end', function(data){
console.log("done");
}

I have not created the databases or collections for this, but can it do this for me with this update? I get an error:

[Error: Connection was destroyed by application]

When I run this, the databases get created but they're empty. Any help would be greatly appreciated.

share|improve this question
 
Why not to use mongoimport docs.mongodb.org/v2.2/reference/mongoimport ? –  Salvador Dali 22 hours ago
 
I would like to create different databases based on criteria inside of the .csv file. The mongoimport seems to allow me to import the entire .csv but not parse it, pull values, and import. –  MonsterWimp757 22 hours ago
 
you want the same csv file to get saved to different databases? –  Asya Kamsky 18 hours ago
 
Yes, the same csv file to different databases based on the data in from one column - so the each unique value in a column would be a different database. –  MonsterWimp757 15 hours ago

1 Answer

If you convert the csv rows in a javascript array (each row is an object), you can use https://github.com/bitliner/MongoDbPopulator

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.