I have User
schema which has a username
field. I would like this field to be case sensitive, so that users may register names such as BobDylan
One way would be to use a pre-save hook to do it.
UserSchema.pre('save', function (next) {
this.username_lower_case = this.username && this.username.toLowerCase();
next();
});
Another way would be to make username
a virtual:
UserSchema.virtual('username').set(function (value) {
this.username_raw = value;
this.username_lower_case = value && value.toLowerCase();
}).get(function () {
return this.username_raw;
});