Mean.js,Mongoose,mongodb:如何使用Tree结构(嵌套数组)将数据更新/插入到集合中?

时间:2014-11-19 14:48:39

标签: node.js mongodb express mongoose mean-stack

我是mean.js堆栈的新手。所以,请原谅我的语义障碍。我有以下结构的mongoose数据模型:

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'
    }
});

如您所见,有嵌套数组。因此,每个文档基本上都是一个树,其中每个扇区文档必须存储多个子扇区数据,每个子扇区元素都有多个行业元素(数组)等等。所以基本上它是一个多维数组。我正在尝试将具有以下列结构的csv文件中的数据加载到我的db:

工业内,代码内,行业,I代码,分部门,SS代码,扇区,S代码

我在服务器端controller.js中编写了以下函数来读取csv文件并在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
            });
        });
    });
};

代码仅在数据库中创建具有第一个树级别的文档。一个样本部门收集文件:

{

    "_id" : ObjectId("546ca30677e06d812cc8da19"),
    "created" : ISODate("2014-11-19T14:02:46.687Z"),
    "subSectors" : [ ],
    "code" : "2000",
    "name" : "Industrials",
    "__v" : 0
}

如您所见,子部门和更远的部分表示为空数组。  任何Mean.js投注者和主人都可以指导吗?

0 个答案:

没有答案