如何在mongoose中添加模式方法?

我一直在试图找出如何在Mongoose中添加模式方法,这将使用模型属性并以某种方式修改它们。 是否有可能使代码在下面的工作?

var mySchema = new Schema({ name: { type: String }, createdAt: { type: Date, default: Date.now }, changedName: function () { return this.name + 'TROLOLO'; } }); 

 MySchema.findOne({ _id: id }).exec(function (error, myschema) { myschema.changedName(); }); 

我想是的,你想要实例方法吗? 这是你的意思与模式方法? 如果是这样,你可以做一些事情:

 var mySchema = new Schema({ name: { type: String }, createdAt: { type: Date, default: Date.now } }); mySchema.methods.changedName = function() { return this.name + 'TROLOLO'; } Something = mongoose.model('Something', mySchema); 

你可以这样做:

 Something.findOne({ _id: id }).exec(function (error, something) { something.changedName(); });