I'm trying to save some documents using node.js/mongoose.
I retrieve the data from a csv file and use the csv module to import the data.
Somehow it says all the records are saving correctly to the DB but when I open it through 'mongo' command and parse the DB no info has been saved at all.
This is the code I'm using:
//import.js
var csv = require('csv');
var mongoose=require("mongoose"),
db = mongoose.createConnection('127.0.0.1','camelot');
db.on('error',function(){
console.error.bind(console,'conection error');
console.log("Error al crear conexion");
});
db.once('open',function(){
console.log("DB open")
// Definicion de jugador
var playerSchema = new mongoose.Schema({
nombre: String,
alianza: String,
correo: String,
poder: Number,
nivel: Number,
villa1: {
nombre: String,
x: Number,
y: Number
},
villa2: {
nombre: String,
x: Number,
y: Number
}
});
var jugador = db.model('jugador',playerSchema);
csv().fromPath('datos.csv',{columns: ['timestamp', 'nombre','alianza','x','y','nivel','poder','notas']})
.on('end',function(count){
console.log('Lineas:'+count)
})
// --> PARSEO DE CADA LINEA DEL CSV <--//
.on('data',function(data,index){
var j = new jugador({ nombre: data.nombre });
j.save(function (err){
if(err){
console.log('Error al escribir en la DB');
}else
{
//console.log("Guardado: "+data.nombre);
}
})
})
});`
Mongoose pluralizes model name by default. You can set collection name manually by passing third argument to db.model if you need this:
var jugador = db.model('jugador',playerSchema, 'jugador');