架構方法

可以在 Schemas 上設定方法,以幫助處理與該模式相關的事情,並使它們井井有條。

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

用法示例:

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