如何将字段推入嵌入式文档

时间:2015-02-24 14:25:03

标签: node.js mongodb mongoose

模型

var ModuleSchema = new Schema({
    name: String,
    types: [{
        name: String,
        functions: [{
            name: String
        }]
    }]    
});

var CoverageSchema = new Schema({
    name: String,
    modules: [{
        name: String,
        types: [{
            name: String,
            functions: [{
                name: String,
                coverages: [{
                    blocksCovered: Number
                }]
            }]
        }]    
});

mongoose.model('Module', ModuleSchema);
mongoose.model('Coverage', CoverageSchema);

coverage.server.controller.js

coverage.modules = []; // Mongoose CoverageSchema existing object.

Module.findOne(...).exec(function(err, module) {
     // If not error
     coverage.modules.push({
         name: module.name, 
         types: module.types
     });

     // How do I add 'coverage.modules.types.functions.coverages: [] ?
     coverage.modules.forEach(function(mod){
         mod.types.forEach(function(type){
            type.functions.forEach(function(func){
                func.coverages.push({blocksCovered: 0}); // Error: coverages undefined.
            });
         });
     });

     coverage.save(function (err) ...);
});

我可以使用ModuleSchema中插入的模块保存coverage对象。 但是在保存之前无法添加modules.types.functions.coverages嵌入文档。

0 个答案:

没有答案