I would update a collection setting the value only if the new values are not null. I have a code like this:
...
var userName = req.body.nome;
var userSurn
The answer by Hüseyin BABAL is on the right track, but that will generate a warning from mongo because calling new User()
will create a new _id
which is immutable. What you want to do is the following:
const userModel = Object.assign(req.body);
User.update({_id: req.session.userID}, userModel, {upsert: true},
function(err){
console.log("Error occured!");
});