Yss
Yss

Reputation: 111

MongoDB NodeJS update removes attributes

When I'm trying to update a single attribute of a user's document after the update query every attribute of the user is missing except the _id and updated attribute. What's wrong with my query?

dbwrapper.mongo.getConnection().then(function(db){
    db.collection('users').update({'_id' : dbwrapper.mongo.ObjID(userID)}, {'iconID':2}, function(error, resultMongo){
      console.log(error);
      if(error || !resultMongo){
        reject(error);
      }else{
        resolve(resultMongo);
      }
    });
});

Upvotes: 0

Views: 45

Answers (1)

robertklep
robertklep

Reputation: 203419

That's how updates work with MongoDB: if the second ("update") document contains only field:value expressions, the document stored in the database will be replaced by the update document. This is documented here.

If you merely want to update the iconID field, use $set:

.update({ '_id' : ... }, { $set : { iconID : 2 }}, ...)

Upvotes: 2

Related Questions