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
Are you not just asking to pass in all the fields that you posted? Why not do this then?
(And basically just a cut and paste of your code):
collection.update(
{_id: ObjectId(req.session.userID)},
{$set: req.body }
)
Then whatever content you posted as fields is set within your update.
Note that use of set will only overwrite, or add new fields. If you just want to replace the whole document, then remove the whole {$set: (..) }
notation and just pass in req body as it's a valild object.
You could try something like this:
var objForUpdate = {};
if (req.body.nome) objForUpdate.nome = req.body.nome;
if (req.body.cognome) objForUpdate.cognome = req.body.cognome;
if (req.body.indirizzo) objForUpdate.indirizzo = req.body.indirizzo;
//before edit- There is no need for creating a new variable
//var setObj = { $set: objForUpdate }
objForUpdate = { $set: objForUpdate }
collection.update({_id:ObjectId(req.session.userID)}, objForUpdate })
If there is not any field that you dont want users to be able to change. since this method will take any value which is not empty and update it. you can do it like this.
const updatedFields = {};
Object.keys(req.body).forEach(key => {
if (!isEmpty(req.body[key])) {
updatedFields[key] = req.body[key];
}
});
YourModel.findOneAndUpdate(
{ employee_id: req.body.employee_id },
{$set:updatedFields},
{new:true}).then(updatedObj =>{
console.log("updated obj:",updatedObj);
})
is empty function
const isEmpty = value =>
value === undefined ||
value === null ||
(typeof value === "object" && Object.keys(value).length === 0) ||
(typeof value === "string" && value.trim().length === 0);
Probably you've got already user authenticated so you should have req.user.*
in this case you can use ternary operator to assign the value and update it with either new one or the current one (so there is no update)
var userName = req.body.nome ? req.body.nome : req.user.nome;
var userSurname = req.body.cognome ? req.body.nome : req.user.cognome;
var userAddress = req.body.indirizzo ? req.body.indirizzo : req.user.indirizzo;
collection.update(
{_id:ObjectID(req.session.userID)},
{$set: { nome: userName, cognome: userSurname, indirizzo: userAddress }}
)
If you don't have req.user then you can do it in 3 steps. 1. find user in collection 2. get current data 3. update data with new or current (as above)
let currentName
let currentSurname
db. collection.findOne({_id: ObjectID(req.session.userID)}, (err, user) => {
if (err) { } // handle error here
else if (user) {
currentName = user.name
currentSurname = user.surname
}
})
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!");
});
You can use lodash like this other question: https://stackoverflow.com/a/33432857/4777292
_.pickBy({ a: null, b: 1, c: undefined }, _.identity);
would be
{b:1}