如何使用 mongoose 中的中间件从一个模式中获取值并在其他模式中更新

How to fetch value from one schema and update in other using middlewares in mongoose

在更新 Movie 文档时,从 User 集合中获取值并将其与在 Movie 中更新的文档一起保存。

Movie.pre('update', function (next) {
   var id = this._update.$set.userid;
   var some = this;
   User.findOne({userid: id}, function(err, post) {
       // how should we add the firstname to current movie document and
       let update query update it all. Something like this...
       some.firstname = post.firstname;
       next();
   });
});

// update query
Movie.statics.saveMovieDetails = function(requestData, callback) {
this.update({userid:requestData.userid},
    { $set:{
            userid   : requestData.userid,
            name     : requestData.name,
            location : requestData.location,
            language : requestData.language,
            date     : requestData.date
        }
    },{upsert:true}, callback);
};

我就是这样做的。如果我错了或者有更好的方法来解决这个问题,请纠正我。

Movie.pre('update', function (next) {
  var id = this._update.$set.userid;
  var some = this;
  User.findOne({userid: id}, function(err, post) {
      some._update.$set.firstname = post.firstname;
      next();
  });
});