Yss Yss - 2 months ago 6
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?

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

Answer

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