node.js - Mean.js, Mongoose , mongodb : How do I update/ insert data into a collection with Tree structure( nested arrays)? -
i new mean.js stack. so, please forgive semantic hiccups. have mongoose data model of following structure :
var sectorschema = new schema({ name: { type: string, default: '', trim: true }, code: { type: string, default: '', trim: true }, subsectors: [{ name: { type: string, default: '', trim: true }, code: { type: string, default: '', trim: true }, industries: [{ name: { type: string, default: '', trim: true }, code: { type: string, default: '', trim: true }, inindustries: [{ name: { type: string, default: '', trim: true }, code: { type: string, default: '', trim: true } }] }] }], created: { type: date, default: date.now }, user: { type: schema.objectid, ref: 'user' } });
as can see there nested arrays. each document tree, each sector document has store multiple subsector data , each subsector element has inturn mutiple industries elements(array) , on. it's multi dimensional array. trying load data csv file following column structure db:
in-industry, in-code, industry, i-code, subsector, ss-code, sector, s-code
i have written following function in server side controller.js read csv file , create tree in mongodb :
exports.upload = function (req, res) { var filename = ''; var target_path = '', headerflag = 0; var form = new formidable.incomingform(); form.parse(req, function (err, fields, files) { var version = 0, tmp_path = files.file.path; filename = files.file.name; // console.log(filename); target_path = config.path + filename; // console.log(target_path); fs.rename(tmp_path, target_path, function (err) { if (err) throw err; fs.unlink(tmp_path, function () { if (err) throw err; //code csv.frompath(target_path) .on('record', function (data) { sector.find({}, { 'code': data[7], 'subsectors.code': data[5], 'subsectors.industries.code': data[3], 'subsectors.industries.inindustries.code': data[1] }, function (err, result) { if (result != '') {} else { sector.find({}, { 'code': data[7], 'subsectors.code': data[5], 'subsectors.industries.code': data[3] }, function (err, result1) { if (result1 != '') { sector.update({ 'code': data[7], 'subsectors.code': data[5], 'subsectors.industries.code': data[3] }, { 'subsectors.industries.inindustries.code': data[1], 'subsectors.industries.inindustries.name': data[0] }); } else { sector.find({}, { 'code': data[7], 'subsectors.code': data[5], }, function (err, result2) { if (result2 != '') { sector.update({ 'code': data[7], 'subsectors.code': data[5] }, { 'subsectors.industries.code': data[3], 'subsectors.industries.name': data[2], 'subsectors.industries.inindustries.code': data[1], 'subsectors.industries.inindustries.name': data[0] }); } else { sector.find({}, { 'code': data[7] }, function (err, result3) { if (result3 != '') { sector.update({ 'code': data[7] }, { 'subsectors.code': data[5], 'subsectors.name': data[4], 'subsectors.industries.code': data[3], 'subsectors.industries.name': data[2], 'subsectors.industries.inindustries.code': data[1], 'subsectors.industries.inindustries.name': data[0], }); } else { var newsector = new sector({ 'name': data[6], 'code': data[7], 'subsectors.name': data[4], 'subsectors.code': data[5], 'subsectors.industries.name': data[2], 'subsectors.industries.code': data[3], 'subsectors.industries.inindustries.name': data[0], 'subsectors.industries.inindustries.code': data[1] }); newsector.save(function (err) { if (err) { console.log(err); } }); /* sector.insert({ 'subsectors.industries.inindustries.code': data[1], 'subsectors.industries.inindustries.name': data[0], 'subsectors.industries.code': data[3], 'subsectors.industries.name': data[2], 'subsectors.code': data[5], 'subsectors.name': data[4], 'name': data[7], 'code': data[6] });*/ } }); } }); } }); } }); }).on('end', function () { console.log('sectors db updated'); res.status(200).send(); }); // till here }); }); }); };
the code creates documents first tree level in database. sample sector collection document :
{ "_id" : objectid("546ca30677e06d812cc8da19"), "created" : isodate("2014-11-19t14:02:46.687z"), "subsectors" : [ ], "code" : "2000", "name" : "industrials", "__v" : 0 }
as can see, subsectors , beyond represented empty array. can mean.js punters , masters please guide ?
Comments
Post a Comment