我一直在尝试找出如何在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();
});