当创build一个新的文档时,只能调用Mongoose Setter?

我使用Mongoose getter和setter我最近发现setter只适用于当我创build一个新的文档(UserSchema.create …)但setter不会被调用更新(UserSchema.findByIdAndUpdate …)。 我是对的还是我错过了什么 如果我是对的,是否有更新?

( http://mongoosejs.com/docs/2.7.x/docs/getters-setters.html

谢谢。

更新

我发现git问题: https : //github.com/LearnBoost/mongoose/issues/751 ,这意味着这是预期的行为。 不知道我是对的。

发现我不应该使用findByIdAndUpdate,而应该查询对象,编辑和保存。

find一个很好的方法,从https://groups.google.com/forum/?fromgroups=#!topic/mongoose-orm/8AV6aoJzdiQ封装:

function findByIdAndSave( model, id, data, next ){ model.findById( id, function( err, doc ) { if( err ){ next( err, null ); } else { if(! doc ){ next( new Error("Object to save not found"), null ); } else { // There must be a better way of doing this for( var k in data ){ doc[k] = data[k]; } doc.save( next ); } } }); } 

(这真的属于一个评论,但答案允许更好的代码格式)