mongoose枚举数
我需要在模式中获得字段的枚举值
我有架构:
let adminSchema = new Schema({ login: { type: String, unique: true, required: true, minlength: 5, maxlength: 300 }, hashedPassword: { type: String }, role: { type: Number, enum: [0, 1, 2], default: 1 }, salt: { type: String } }); module.exports.Admin = Admin; module.exports.roleEnum = Admin.schema.path('role').enumValues; console.log(module.exports.roleEnum);
控制台日志 – >未定义
但如果我改变angular色字段types为string
let adminSchema = new Schema({ login: { type: String, unique: true, required: true, minlength: 5, maxlength: 300 }, hashedPassword: { type: String }, role: { type: String, enum: ['0', '1', '2'], default: '1' }, salt: { type: String } }); module.exports.Admin = Admin; module.exports.roleEnum = Admin.schema.path('role').enumValues; console.log(module.exports.roleEnum);
控制台日志 – > ['0','1','2'];
我怎样才能获得枚举数组types?
要指定数值范围,可以在模式中定义min
和max
:
role: { type: Number, min: 0, max: 2, default: 1 },
文档在这里 。
还要求这些值是整数,参见这里 。
这里的枚举基本上是String对象。 他们不能是数字
-
所有SchemaTypes都有内置的必需validation器。所需的validation器使用SchemaType的checkRequired()函数来确定该值是否满足所需的validation器。
-
数字有最小和最大的validation器。
- string具有枚举,匹配,最大长度和最小长度validation器。