我不能将数组推入mongoose模式
这是我的控制器,一个表格发送到这里的数据:
exports.addrepair = function(req, res, next){ Person.findById( req.body.id, function (err, Person) { Person.update({id: req.body.id}, {$pushAll: { problem: req.body.problem , solution: req.body.solution , date_on: Date.now() }} ,{upsert:true},function(err){ if(err){ console.log(err); }else{ console.log("Added"); } }) }) }
该模式是:
var Person = new Schema ({ name: String, Repair: [ problem: String, solution: String, date_on: Date ] })
并不能推动任何修复人。 与console.log我可以看到所有的作品,但不是推。
exports.addrepair = function(req, res, next) { //The Person.findById you had here was unnecessary/useless Person.findByIdAndUpdate(req.body.id, { Repair: { $push: { problem: req.body.problem, solution: req.body.solution, date_on: Date.now() } } }, //You don't want an upsert here function(err){ if(err){ console.log(err); }else{ console.log("Added"); } } ) }
这现在适合我。 Thaks Peter Lyons
Person.findByIdAndUpdate(req.body.id, { $push: { repair: { problem: req.body.problem , solution: req.body.solution , date_on: Date.now() } }, function(err){ if(err){ console.log(err) } else { console.log('Added') } });