node.js - Mongoose Updating Certain Fields of a Document -
i'm trying implement update method our api , i'm kinda new node didn't know best practice carry out task of updating fields of document. let me elaborate, have user model keeps basic info of user name, age, sex, school, bio, birthday etc. our update method should work this, request of method includes new values of fields provided such {bio:'newbio'} or {school:'newschool', name:'newname'} must update provided fields provided data , leave rest are. wondering best approach problem @ hand be. in advance
the easiest approach can think of use $set perform update operations.
an example :
var updatedusers= function(db, callback) { db.collection('users').updatemany( { "_id": "value"}, { $set: { bio: "new bio" } } , function(err, results) { console.log(results); callback(); }); };
and invoke above function :
mongoclient.connect(url, function(err, db) { assert.equal(null, err); updatedusers(db, function() { db.close(); }); });
Comments
Post a Comment