Javascript 使用mongoose更新许多属性的优雅方式
我有一个有7个属性的模型,当前端有编辑请求时,我想更新所有属性。有没有什么优雅的方法可以做到这一点,或者我必须像在下面的代码中那样手动键入所有这些代码(顺便说一句,whitch对我来说很好,但看起来真的很难看)Javascript 使用mongoose更新许多属性的优雅方式,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我有一个有7个属性的模型,当前端有编辑请求时,我想更新所有属性。有没有什么优雅的方法可以做到这一点,或者我必须像在下面的代码中那样手动键入所有这些代码(顺便说一句,whitch对我来说很好,但看起来真的很难看) 这将更新现有记录并返回更新后的值。如果没有找到匹配的记录,它将向回调或承诺返回一个错误的值(不记得是null还是其他值) 这将更新现有记录并返回更新后的值。如果没有找到匹配的记录,它将向回调或承诺返回一个错误的值(不记得是null还是其他值) 您可以尝试以下方法: exports.sav
这将更新现有记录并返回更新后的值。如果没有找到匹配的记录,它将向回调或承诺返回一个错误的值(不记得是
null
还是其他值)
这将更新现有记录并返回更新后的值。如果没有找到匹配的记录,它将向回调或承诺返回一个错误的值(不记得是
null
还是其他值)
您可以尝试以下方法:
exports.saveDish = (req, res, next) => {
/**
*
* upsert: true --> helps to insert new document if no matching doc exists
* new: true --> returns new document in output
* rawResult: true --> helps to find out whether update or insert operation is done
*
* Dish is a mongoose schema where findByIdAndUpdate is only from mongoose,
* which internally converts a string from it's first parameter into {_id : ObjectId('req.body._id')}, also uses $set operation on req.body
*
* Print data to check what's being returned, you might see entire document(data.value) being returned with some other information
*
* */
Dish.findByIdAndUpdate(req.body._id, req.body, { upsert: true, new: true, rawResult: true }, (err, data) => {
if (err) { console.log(err); res.status(200).json({ message: 'Operation Failed' }) }
if (data.lastErrorObject.updatedExisting) return res.status(204).json({ message: 'Dish data properly updated' });
return res.status(201).json({ message: 'New dish properly saved' });
})
};
在这里,您正在更新现有文档(添加新字段或更新现有字段w.r.t.what's are req.body),或者在数据库中找不到匹配的\u id
时插入整个新文档,这样可以避免多次数据库调用。在这里,我是在回调中实现的,但之前我实际上是在async
wait
中实现的,无论哪种方式都可以,这应该适用于上面列出的所有情况
Ref:您可以尝试以下方法:
exports.saveDish = (req, res, next) => {
/**
*
* upsert: true --> helps to insert new document if no matching doc exists
* new: true --> returns new document in output
* rawResult: true --> helps to find out whether update or insert operation is done
*
* Dish is a mongoose schema where findByIdAndUpdate is only from mongoose,
* which internally converts a string from it's first parameter into {_id : ObjectId('req.body._id')}, also uses $set operation on req.body
*
* Print data to check what's being returned, you might see entire document(data.value) being returned with some other information
*
* */
Dish.findByIdAndUpdate(req.body._id, req.body, { upsert: true, new: true, rawResult: true }, (err, data) => {
if (err) { console.log(err); res.status(200).json({ message: 'Operation Failed' }) }
if (data.lastErrorObject.updatedExisting) return res.status(204).json({ message: 'Dish data properly updated' });
return res.status(201).json({ message: 'New dish properly saved' });
})
};
在这里,您正在更新现有文档(添加新字段或更新现有字段w.r.t.what's are req.body),或者在数据库中找不到匹配的\u id
时插入整个新文档,这样可以避免多次数据库调用。在这里,我是在回调中实现的,但之前我实际上是在async
wait
中实现的,无论哪种方式都可以,这应该适用于上面列出的所有情况
Ref:@Eddieden,你的方法工作得很好,结果是你必须将任何id传递给findByIdAndUpdate()方法,所以我对它进行了一些编辑,以处理独特的新菜肴 工作代码以防万一:
exports.saveDish = (req, res, next) => {
if (req.body._id) {
Dish.findByIdAndUpdate(
{ _id: req.body._id },
{ ...req.body },
{ useFindAndModify: false }
)
.then(oldDish => {
if (oldDish) {
oldDish.save();
return res
.status(204)
.json({ message: 'Dish data properly updated' });
}
})
.catch(err => console.log(err));
} else {
const newDish = new Dish(req.body);
newDish
.save()
.then(result => {
return res.status(201).json({ message: 'New dish properly saved' });
})
.catch(err => console.log(err));
}
};
@EddieDean,你的方法工作得很好,结果是你必须将任何id传递给findByIdAndUpdate()方法,所以我对它进行了一些编辑,以处理独特的新菜肴 工作代码以防万一:
exports.saveDish = (req, res, next) => {
if (req.body._id) {
Dish.findByIdAndUpdate(
{ _id: req.body._id },
{ ...req.body },
{ useFindAndModify: false }
)
.then(oldDish => {
if (oldDish) {
oldDish.save();
return res
.status(204)
.json({ message: 'Dish data properly updated' });
}
})
.catch(err => console.log(err));
} else {
const newDish = new Dish(req.body);
newDish
.save()
.then(result => {
return res.status(201).json({ message: 'New dish properly saved' });
})
.catch(err => console.log(err));
}
};
您是否尝试过Mongoose模型方法
findByIdAndUpdate
?听起来像是你想要的。你试过Mongoose模型方法吗?听起来像是你想要的。