如何使用mongoose更新节点中的内部文件

我想在pro_picture中插入cont和cont_type。 但是这不是一个embedded式模式。

我的模式:

var userSch = new Schema({ name: String, email: String, profile_picture: { cont: String, cont_type: String } }); var User = mongoose.model('user', userSch); 

现在我想查找电子邮件地址为“something@example.com”的用户并更新pro_picture。

我试了下面的代码。

  User.findOne({email: data.email}, function (err, user) { var userData = new User({ profile_picture: { cont: data.pro_cont, cont_type: data.pro_cont_type } }); user.profile_picture = userData; console.log(user.profile_picture); console.log(user); user.save(function (err) { if(err) throw err; callback(rjTypes.result.SUCCESS); }); 

表明

 profile_picture: null //in console 

但该领域不存在收集

终于我find了..

我通过使用这种技术解决了….

 User.findOne({email: data.email}, function (err, user) { user.profile_picture.cont = data.pro_cont; user.profile_picture.cont_type = data.pro_cont_type; console.log(user.profile_picture); console.log(user); //user.profile_picture.cont_type = data.pro_cont_type; user.save(function (err) { if(err) throw err; callback(rjTypes.result.SUCCESS); }); }); 

我认为你在做一个嵌套,尝试改变

 var userData = new User({ profile_picture: { cont: data.pro_cont, cont_type: data.pro_cont_type } }); 

 var userData = { cont: data.pro_cont, cont_type: data.pro_cont_type };