1.mongoose预定义模式修饰符
var UserSchema = mongoose.Schema({
name:{
type: String,
trim: true
},
age:Number,
status:Number
})
2.mongoose自定义模式修饰符
- 1.
Getters
:从数据库获取的数据name
字段拼接@
符号
var UserSchema = mongoose.Schema({
name:{
type: String,
get(params){
return '@' + params
}
},
age:Number,
status:Number
})
- 2.
Setters
:插入数据库的数据name
字段必须拼接test-
var UserSchema = mongoose.Schema({
name:{
type: String,
set(params){
if(!params){
return params;
} else {
if (params.startsWith('test-')) {
return params
} else {
return 'test-' + params
}
}
}
},
age:Number,
status:Number
})