[英]Delay when updating document (MongoDB/Mongoose)
In my application, I am attempting to update a object nested in an array as a below.在我的应用程序中,我试图更新嵌套在数组中的 object,如下所示。 When testing in postman, there is a delay causing me to have to make two requests in order to see the updated value.在 postman 中进行测试时,延迟导致我必须发出两次请求才能看到更新的值。
if (taskStatus) {
const taskStatusNew = await Board.findOneAndUpdate(
{
"columns.tasks._id": req.params.id,
},
{
$set: {
"columns.$[].tasks.$[t]": req.body,
},
},
{
arrayFilters: [
{
"t._id": req.params.id,
},
],
}
);
res.status(200).json(taskStatusNew);
}
By default, findOneAndUpdate()
returns the document as it was before the update was applied.默认情况下, findOneAndUpdate()
返回应用更新前的文档。 So you have to set the new
option to true
if you are using mongoose.因此,如果您使用 mongoose,则必须将new
选项设置为true
。
const taskStatusNew = await Board.findOneAndUpdate(
{
"columns.tasks._id": req.params.id,
},
{
$set: {
"columns.$[].tasks.$[t]": req.body,
},
},
{
arrayFilters: [
{
"t._id": req.params.id,
},
],
new: true
}
);
Documentation article for reference: https://mongoosejs.com/docs/tutorials/findoneandupdate.html参考文档文章: https://mongoosejs.com/docs/tutorials/findoneandupdate.html
If your question is like to return the updated value then use this,- {returnDocument: 'after'} , you just need to add this in other parameter, then it will give you updated value.如果您的问题是返回更新后的值,请使用它,- {returnDocument: 'after'} ,您只需将其添加到其他参数中,它就会为您提供更新的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.