MichalV
MichalV

Reputation: 1

How to use findByIdAndUpdate on mongodb?

I am a noobie in coding and I am having an issue with how to use properly MongoDB. I have a parent object classroom containing an array of objects - comments. I am trying to update the content of 1 selected comment.

originally I updated the state of the whole "classroom" in the react and passed all the data and $set {req.body} in findByIdAndUpdate.

I want to achieve the same result if I only pass to my axios request classId, commentId and comment data and not whole classroom / all comments

I tried to filter selected comment out of the array of comments and concat updated comment, but that did not work. Clearly, I have any idea what is going on and docs don't make it any easier for me to understand.

my classroom schema:

var ClassroomSchema = new Schema({
  title: String,
  teacher: String,
  info: String,
  image_url: String,
  comments: [Comment.schema]
});

comment schema:

var CommentSchema = new Schema()

CommentSchema.add({
    content: String,
    comments: [CommentSchema],
    created_at: {
        type: Date,
        default: Date.now
    }
});

original solution:

function update(req, res){
    Comment.findById(req.params.comment_id, function(err, comment) {
        if(err) res.send(err)
        comment.content = req.body.content;
        comment.save();
        console.log(req.body.comments)
        Classroom.findByIdAndUpdate(req.params.classroom_id,
            {$set: req.body}, function(err, classroom){
            if (err) {
                console.log(err);
                res.send(err);
            } else {
                commentToUpdate = req.body.commentData;
                res.json(classroom);
            }
        });
    });
}

my current failing atempt:

function update(req, res){
    console.log('update => req.body: ', req.body);
    console.log('req.params', req.params)
    Comment.findById(req.params.comment_id, function(err, comment) {
        if(err) res.send(err)

        comment.content = req.body.content;
        comment.save();
        console.log('comment: ', comment);

        Classroom.findById(req.params.classroom_id, function(err, classroom) {
                console.log('CLASSROOM findByIdAndUpdate classroom: ', classroom)
                // console.log('reg.body: ', req.body)
            if (err) {
                console.warn('Error updating comment', err);
                res.send(err);
            } else {
                // commentToUpdate = req.body.commentData;
                old_comments = classroom.comments;
                console.log('comments: ', old_comments);

                Classroom.findByIdAndUpdate(req.params.classroom_id, 
                        {$set: 
                            { comments: old_comments.filter(comt._id !== comment._id).concat(comment)} 
                        }, function(err, updatedClassroom) {
                            if (err) {
                                console.warn(err);
                            } else {
                                res.json(updatedClassroom);
                            }
                }); 
            }
        });
    });
}

Upvotes: 0

Views: 311

Answers (1)

IRSHAD
IRSHAD

Reputation: 1622

haven't tested, but try this.

function update(req, res) {
  Classroom.update(
    { _id: req.params.classroom_id, "comments._id": req.params.comment_id },
    { $set: { "comments.$.content": req.body.content } },
    function(err) {
     ..
    }
  );
}

Upvotes: 0

Related Questions