0

I have a simple mongodb model that i want to update.

const ProfileScheme = Schema({ 
 nationality:{
    nation: {type: String,lowercase: true, default: null},
    town: {type: String,lowercase: true,  default: null},
    privacy: {type: Number, min:1, max:3, default: 2} 
 },///etc

I found out that updating the nation document is difficult than expected. Can anyone help please. Because I have try this.

let nationality = req.body.nationality,
      privacy = req.body.privacy; res.redirect(req.get('referer'));
      updateNationality(profile, nationality, privacy,
          function(err, info){
             if(err){next(err);}
                   console.log(info);
             });
            res.redirect(req.get('referer'));

updateNationality: function(profile,nation, privacy, cb){
    let nationality = {}; 
    nationality.nation = nation;
    nationality.privacy = privacy;console.log(nationality);console.log("Profile  is loading "+profile);// All the log are showing the expected data
    Profile.update(profile.nationality, {$set: { 'profile.$.nationality': nationality } }, cb); //this is not working
},
Kabiru Wahab
  • 77
  • 1
  • 13
  • Just `{$set: { 'profile.nationality': nationality }`. Most people get this wrong the other way around or want to name the "key" dynamically. But it's just the dot `.` without the positional `$` operator. – Neil Lunn Apr 29 '18 at 01:24
  • Specifically [this answer](https://stackoverflow.com/a/16119136/2313887) on the duplicate question, as sadly most of the responses there are not actually correct. That one is. – Neil Lunn Apr 29 '18 at 01:26

0 Answers0