2016-03-07 179 views
0

我工作的一個項目,我有以下貓鼬架構模型對象:貓鼬不創建數組

module.exports = mongoose.model('Env', { 
    name : {type : String, default: ''}, 
    services: [ServiceAddress] 
}); 

module.exports = mongoose.model('ServiceAddress', { 
    serviceId : {type : ObjectId}, 
    address: {type: String} 
}); 

我試圖更新它有一個空的現有的「信封」的文件「服務」陣列。

router.put('/api/env', function(req, res, next) { 
    var env = new Environment(req.body); 
    env.save(function(err, updated){ 
     if (err){ 
      res.send(err); 
     } 
     res.json(updated); 
    }); 
}); 

的req.body內容是:

{ 
    _id: "56dd26a6618c1b983d5bada6", 
    name: "Test Drive" 
    services: [{_id: "56dc6e385fb0b038241d3399", address: "11"}, {_id:  "56dc6e595fb0b038241d339a", address: ""}] 
} 

但結果在DB是(僅ID被推):

"services" : [ { "_id" : ObjectId("56dc6e385fb0b038241d3399") }, { "_id" : ObjectId("56dc6e595fb0b038241d339a") } 

任何想法?

感謝

回答

0

請嘗試用下面的代碼片段

var serviceSchema = { 
    serviceId : {type : ObjectId,required: false}, 
    address: {type: String,required: false} 
}; 
// added so that new _id field is not added when object is pushed in array 
serviceSchema = 'new Schema('+serviceSchema +',{_id:false})'; 

module.exports = mongoose.model('Env', { 
    name : {type : String, default: ''}, 
    services: [serviceSchema], default:[], 
}); 

感謝