我的mongoose纲要是不正确的?

我相信我的Mongoose Schema必须是不正确的,因为我在Mongoose中使用.save()时没有正确更新我的数据库。 在下面的nodeJS路由运行之后,callback中接收到的更新对象是正确的,但在后续运行中,DB对象是空的,并且从未更新过。

以下是我的数据看起来像的一个例子:

var newApplicationData = new ApplicationData({ pos1: 0, pos2: 0, applicationData: [ // Standard One { sections: [ { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] } ] }, // Standard 2 { sections: [ { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] } ] }, // Standard 3 { sections: [ { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] } ] }, // Standard 4 { sections: [ { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] }, { narrative: '', documentation: [] } ] }, ] }); 

现在这是我的Mongoose Schema:

 const mongoose = require('mongoose'); const ApplicationDataSchema = mongoose.Schema({ pos1: { type: Number, required: true, }, pos2: { type: Number, required: true, }, applicationData: { type: Array, required: true, sections: { type: Array, required: true, narrative: { type: String, required: true }, documentation: { type: Array, required: true } } } }, { collection : 'applicationData' }); const ApplicationData = module.exports = mongoose.model('ApplicationData', ApplicationDataSchema); 

最后,这里是我正在尝试使用.save()保存数据的NodeJS路线:

 app.post('/upload', multer({ storage: storage }).single('file'), (req, res) => { if(!req.file) return res.send({ status: 400, msg: 'No File Received.' }); let pos1 = req.body.pos1; let pos2 = req.body.pos2; ApplicationData.findById(mongoose.Types.ObjectId(req.body.applicationDataID), (err, applicationDataObject) => { if(err) return res.send({ status: 400, msg: err }); applicationDataObject.applicationData[pos1].sections[pos2].documentation.push(req.file.filename); applicationDataObject.save((err, updatedApplicationData) => { console.log(applicationDataObject.applicationData[pos1].sections[pos2].documentation); if(err) return res.send({ status: 400, msg: err }); return res.send({ status: 200, applicationData: updatedApplicationData.applicationData }); }); }); }); 

当您使用array of object ,请将object description放入type

  { pos1: { type: Number, required: true, }, pos2: { type: Number, required: true, }, applicationData: { type: [{ sections: { type: [{ narrative: { type: String, required: true, }, documentation: { type: Array, required: true, }, }], required: true, }, }], required: true, }, } 

最好是使用子模式寿,如:

 const subSchema2 = new mongoose.Schema({ narrative: { type: String, required: true, }, documentation: { type: Array, required: true, }, }) const subSchema1 = new mongoose.Schema({ sections: { type: [subSchema2], required: true, }, }) const finalSchema = new mongoose.Schema({ pos1: { type: Number, required: true, }, pos2: { type: Number, required: true, }, applicationData: { type: [subSchema1], required: true, }, })