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

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -