数据没有填充在猫鼬中

时间:2016-01-28 11:35:20

标签: node.js mongodb mongoose

这是我的架构

var employee =  
    {
        empCode : { type : String  ,  unique: true ,  required : true },
        firstName : { type : String  , required : true },
        lastName : { type : String },
        email :  { type : String  },
        DOJ :{ type : Date , default: Date.now },
        DOB :{ type : Date  },
        phoneNo : { type : String },
        authentication : 
            {
                username : { type : String },
                password : { type : String }
            },
        dsg : 
            [{
                designationID: [{type : mongoose.Schema.ObjectId , ref:'designation'}],
                effFrom :{ type : Date , default: Date.now },
                effTo : { type : Date }             
            }],
        grd : 
            {
                grade : [{type : mongoose.Schema.ObjectId, ref:'grade'}],
                effFrom :{ type : Date , default: Date.now },
                effTo : { type : Date }
            },
        dpt :
            {
                departmentID : [{type : mongoose.Schema.ObjectId ,ref:'department'}],
                effFrom :{ type : Date , default: Date.now },
                effTo : { type : Date }
            },
        manager : 
            {
                managerID : {type : mongoose.Schema.ObjectId , ref:'employee'},
                effFrom :{ type : Date , default: Date.now },
                effTo : { type : Date }
            },
        rol :
            {   
                roleID : [{type : mongoose.Schema.ObjectId , ref:'role'}],
                effFrom :{ type : Date , default: Date.now },
                effTo : { type : Date }
            },

        status : {type:Boolean}
    }

我想像这样填充

empController.prototype.list=function(callback){
    objMdlEmp.find()
    .populate('designationID')
    .populate('grade')
    .exec(function(err, records){
        if(err){
            console.log(err);return;
            callback(false , config.msg.SOMTHING_WRONG);
            return;
        }else {
            callback(true , records);   
        }
    })
};

但未显示其他文档数据, 当我这样我的架构然后只显示它,但我需要添加effFrom和effTo数据

dsg : [{type : mongoose.Schema.ObjectId , ref:'designation'}]

1 个答案:

答案 0 :(得分:1)

您无法在mongoose中填充嵌入字段,但您可以使用一些解决方法

1)将您的相关数据保存在其他字段中

var employee = {
    grd: {type: mongoose.Schema.ObjectId, ref: 'grade'},
    grdData: {
        grade: [],
        effFrom: {type: Date, default: Date.now},
        effTo: {type: Date}
    }
};

2)使用mongoose-deep-populate

等插件
objMdlEmp.find().deepPopulate('grd.grade').exec(function (err, records) { ... });