简体   繁体   中英

MongoDB, update collection field if new value is not null

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 userSurname = req.body.cognome;
 var userAddress = req.body.indirizzo;

 collection.update(
     {_id:ObjectId(req.session.userID)},
     {$set: { nome: userName, cognome: userSurname, indirizzo: userAddress }}
 )

Is there an easy way for doing this?

ANOTHER WAY: if I could take the value req.body.* from the placeholder of the form where I take the data, I could solve the problem.. but is this possible?

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 )

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}

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 can use mongoose for that by casting req.body to your model,

I assume you have mongoose model called User , and in your controller,

var userModel = new User(req.body);
User.update({_id: req.session.userID}, userModel, {upsert: true}, function(err){
   console.log("Error occured!");
});

There is no mongoose tag, but I strongly recomment to use that. For more details;

Mongoose Update

Mongoose Model

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);

This version still allows for null string fields to be respected and updated. Omitted fields would be ignored.

const cleanedObject = Object.keys(origObject).reduce((acc, k) => {
  if (typeof origObject[k] === "undefined") return acc;
  acc[k] = origObject[k];
  return acc;
}, {});

collection.update({_id:ObjectId(req.session.userID)}, cleanedObject })

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
    }
})
    let objForUpdate = {}
    for (const key of Object.keys(req.body)) {
       if (req.body[key]) objForUpdate = Object.assign({}, objForUpdate, { [key]: req.body[key] })
    } 
    collection.update({_id:ObjectId(req.session.userID)}, { $set:  objForUpdate })

This will dynamically add fields in objForUpdate if defined in the body.

var userName = req.body.nome;
var userSurname = req.body.cognome;
var userAddress = req.body.indirizzo;

collection.update(
  { _id: ObjectId(req.session.userID) },
  {
    $set: {
      ...userName && { nome: userName },
      ...userSurname && { cognome: userSurname },
      ...userAddress && { indirizzo: userAddress },
    },
  }
)

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!");
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM