I am trying to make an put request to change some information. I get the new informations from request body, however I want some parameters optional
example: old record --> {name: ugur, surname:k}
put request comes like this--> {name: uur}
so i want surname to stay same but it will be null now which i dont want to.
here is the code snippet :
await User.findOneAndUpdate({filter}, {$set:{name:req.body.name, surname:req.body.surname}})
In this way when user dont send a surname, users surname will be updated as null. Is there a way to get users old surname? for instance -->
await User.findOneAndUpdate({filter}, {$set:{name:req.body.name, surname:req.body.surname || this.getsurname}})
I want to solve it without using if else or first findOne then update the user.