Mongodb更新没有数组的嵌套文档

时间:2018-04-29 01:17:57

标签: mongodb

我有一个简单的mongodb模型,我想更新。

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

我发现更新国家文件比预期困难。 任何人都可以帮忙。因为我试过这个。

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

0 个答案:

没有答案