programing

Mongoose findOne 및 업데이트 여러 필드 업데이트

bestprogram 2023. 6. 21. 22:48

Mongoose findOne 및 업데이트 여러 필드 업데이트

findOneAndUpdate 메서드가 제대로 작동하지 않습니다.모든 필드를 한 번에 업데이트하려고 하는데 마지막 필드만 업데이트(설정)하고 있습니다.항상 마지막 필드입니다.제가 무엇을 잘못하고 있는지 또는 기대되는 효과를 얻기 위해 제가 무엇을 할 수 있는지 누가 말해줄 수 있나요?

다음은 나의 findOneAndUpdate 코드입니다.

Book.findOneAndUpdate({_id:bookId},{$set:{"name": name},$set:{"genre": genre},$set:{"author": author},$set:{"similar": similar}}).exec(function(err, book){
       if(err){
           console.log(err);
           res.status(500).send(err);
       } else {
            res.status(200).send(book);
       }
});

연산자를 여러 번 사용하고 있습니다.올바른 구문:$set다음과 같습니다.

{ $set: { <field1>: <value1>, ... } }

다음과 같이 업데이트 인수를 변경해야 합니다.

Book.findOneAndUpdate({ "_id": bookId }, { "$set": { "name": name, "genre": genre, "author": author, "similar": similar}}).exec(function(err, book){
   if(err) {
       console.log(err);
       res.status(500).send(err);
   } else {
            res.status(200).send(book);
   }
});

언급URL : https://stackoverflow.com/questions/37267042/mongoose-findoneandupdate-updating-multiple-fields