更新 mongodb 中的嵌套对象数组时出现问题
Problem with updating nested array of object in mongodb
我正在尝试更新嵌套数组对象中的字段,但该字段未更新。
我需要更新评论字段,我正在使用下面的查询来更新它,使用 mongoose,
let updateComment = await StoryComment.update({ _id: this.req.body.commentId, 'comments.reply.userId': currentUser, 'comments.reply._id': this.req.body.replyId }, { $set:{ 'reply.$.comment': replyData } }, { new: true, 'safe': true, 'upsert': true });
谁能帮我解决这个问题,它看起来不错,但没有更新
请试试这个
{ $set:{ 'comments.reply.$.comment': replyData } }
尝试以下,这可能会满足您的要求
let updateComment = await StoryComment.updateOne(
{
_id: mongoose.Types.ObjectId(this.req.body.commentId),
},
{
$set:{
"comments.reply.$[el].comment": replyData
}
},
{
arrayFilters:[{
"el._id":mongoose.Types.ObjectId(this.req.body.replyId)
}],
new: true
}
)
我正在尝试更新嵌套数组对象中的字段,但该字段未更新。
我需要更新评论字段,我正在使用下面的查询来更新它,使用 mongoose,
let updateComment = await StoryComment.update({ _id: this.req.body.commentId, 'comments.reply.userId': currentUser, 'comments.reply._id': this.req.body.replyId }, { $set:{ 'reply.$.comment': replyData } }, { new: true, 'safe': true, 'upsert': true });
谁能帮我解决这个问题,它看起来不错,但没有更新
请试试这个
{ $set:{ 'comments.reply.$.comment': replyData } }
尝试以下,这可能会满足您的要求
let updateComment = await StoryComment.updateOne(
{
_id: mongoose.Types.ObjectId(this.req.body.commentId),
},
{
$set:{
"comments.reply.$[el].comment": replyData
}
},
{
arrayFilters:[{
"el._id":mongoose.Types.ObjectId(this.req.body.replyId)
}],
new: true
}
)