0
所以我想只更新传递的对象的值。mongoose findbyidandupdate刚通过值
Event.findByIdAndUpdate(req.body.id, {
$set: {
name: req.body.name,
narrative: req.body.narrative,
startdate: req.body.startdate,
enddate: req.body.enddate,
site: req.body.site
}
}, {new: true},
function(err, Event) {
if (err) throw err;
res.send(Event);
});
我现在的函数将null在post请求中没有定义的任何字段。
例如,如果我的对象的所有字段定义,我尝试更新只是名字:
{
"id": "57b8fa4752835d8c373ca42d",
"name": "test"
}
将导致:
{
"_id": "57b8fa4752835d8c373ca42d",
"name": "test",
"narrative": null,
"startdate": null,
"enddate": null,
"site": null,
"__v": 0,
"lastupdated": "2016-08-21T00:48:07.428Z",
"sponsors": [],
"attendees": []
}
有什么办法来执行具有了此更新通过所有其他领域呢?
你确定,你有你'req.body'所有的价值呢? –