0

I am trying to update a field in nested array object, but the field is not getting updated.

enter image description here

I need to update the comment field and I am using the below query to update it, using 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 });

can anyone help me to fix this issue, it looks right but its not getting updated

2 Answers 2

1

Try following, this might meet your requirement

   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
      }
    )
Sign up to request clarification or add additional context in comments.

Comments

0

Please try this

{ $set:{ 'comments.reply.$.comment': replyData } }

4 Comments

Is this doing insert all the time?
its always updating a specific one although the reply._id is different
I guess it's always updating the first reply. You can check with find what document are you getting back as you are running on comments collection that will always return `comment' document only
let me try the find option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.