架构方法

可以在 Schemas 上设置方法,以帮助处理与该模式相关的事情,并使它们井井有条。

userSchema.methods.normalize = function() {
    this.name = this.name.toLowerCase();
};

用法示例:

erik = new User({
    'name': 'Erik',
    'password': 'pass'
});
erik.normalize();
erik.save();