1
我推到我的學生模型數據的問題,它的模式看起來如下:不能推JSON對象到數組使用JavaScript和MongoDB
var StudentSchema = new Schema({
firstName: {
type: String,
trim: true,
default: ''
//validate: [validateLocalStrategyProperty, 'Please fill in your first name']
},
lastName: {
type: String,
trim: true,
default: ''
//validate: [validateLocalStrategyProperty, 'Please fill in your last name']
},
worksnap: {
user: {
type: Object
},
timeEntries : [],
},
timeEntries : []
});
雖然我推項目的JavaScript代碼如下所示:
Student.findOne({
'worksnap.user.user_id': item.user_id[0]
})
.populate('user')
.exec(function (err, student) {
if (err) {
throw err;
}
//student.timeEntries.push(item); // this works
student.worksnap.timeEntries.push(item); // this does not work
student.save(function (err) {
if (err) {
//return res.status(400).send({
// message: errorHandler.getErrorMessage(err)
//});
} else {
console.log('item inserted...');
}
});
});
正如你可以看到,如果我用timeEntries陣列的worksnap對象正常工作之外,它插入該項目爲對象成數組...我只是不知道爲什麼它不工作的同樣在worksnap內部。
是否有任何其他選項,我可以添加JSON對象到一個數組類型蒙戈
感謝
仍然沒有工作後加瘦肉(),它抱怨在student.save()行 –
嘗試'.update',而不是'.save' – Rayon