Yss Yss - 1 year ago 72
Node.js Question

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?

db.collection('users').update({'_id' : dbwrapper.mongo.ObjID(userID)}, {'iconID':2}, function(error, resultMongo){
if(error || !resultMongo){

Answer Source

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 }}, ...)