我有以下代码:
var concert = new models.concert({
name: request.body.name,
date: request.body.date,
city: request.body.city,
location: request.body.location,
programme: request.body.programme
});
console.log("<< Concert to attempt save on: " + concert);
if (request.body._id) {
console.log("Received _id field from body: " + request.body._id);
concert._id = ObjectId(request.body._id + '');
}
但是,日志告诉我:
<< Concert to attempt save on:
{
_id: 572312a289a4d8d810eed717,
programme: 'to be announced',
location: 'J.Nepomuk-Kapelle',
city: 'Vienna, Austria32',
date: 1464382800,
name: 'Chamber Music Concert'
}
文档确实说mongoose会自动为模型分配一个ObjectId
,如果它没有,但我预计它会发生在例如保存/更新,但它似乎发生在模型实例化。我想要的是仅当未从客户端收到一个,或者如果收到_id
值时,我才会自动分配0
。这可能吗?感谢。