给定汽车的以下模式/结构:
car : {
_id: "123123",
name: "Ferrari",
owners: [
{
_id : "098098",
active: true
},
{
_id : "876876",
active: true
}
]
}我想将所有owners.active更改为false
使用下面的代码,我能够保存更新后的car对象,但它不会保存到数据库中:
// find car by id
db_car.findById(req.id).then(function(car){
car.owners.forEach(function(owner){
owner.active = false;
});
car.save();
console.log('updated car', car); // this is correct, shows updated data
});//现在检查数据库,数据没有保存在数据库中!
发布于 2017-06-09 17:27:27
Mongoose .findById()返回一个查询。Mongoose查询是而不是Mongoose承诺。相反,您可以使用.exec()执行查询,该查询返回一个promise。例如:
db_car.findById(req.id).exec().then(function(car){ ...相关文档:http://mongoosejs.com/docs/promises.html#queries-are-not-promises
https://stackoverflow.com/questions/44454080
复制相似问题