mongoose纲要号字段准确的长度
我有这个moongoose模式:
var userSchema = new mongoose.Schema({ firstname: { type: String, required: true, min: 3, max: 24 }, lastname: { type: String, required: true, min: 3, max: 24 }, id: { type: Number, required: true, min: 9, max: 9 }, mediations: [assetSchema] });
当我尝试添加一个新的用户ID为320981350我得到下一个validation错误:
{ "errors": { "id": { "message": "Path `id` (320981350) is more than maximum allowed value (9).", "name": "ValidatorError", "properties": { "max": 9, "type": "max", "message": "Path `{PATH}` ({VALUE}) is more than maximum allowed value (9).", "path": "id", "value": 320981350 }, "kind": "max", "path": "id", "value": 320981350, "$isValidatorError": true } }, "_message": "User validation failed", "message": "User validation failed: id: Path `id` (320981350) is more than maximum allowed value (9).", "name": "ValidationError" }
是否有任何其他方式来validationNumber
types字段是在一个确切的长度? 还是我误解了mongoose店的数字?
min
和max
并不意味着所提供的数字中允许的数字量,因为错误说:
(320981350)超过最大允许值 (9)
他们的意思是Number
types的字段的实际最小值/最大值,例如在
{ type: Number, min : 101, max : 999 }
- 允许的最大
Number
是999
- 允许的最小
Number
是101
在你的情况下,如果你有9位数字作为你的id
,在模式中定义字段如下:
{ type: Number, min : 100000000, max : 999999999 }
min
或max
不等于min-length
或max-length
…
所以为了实现你的目标,你最好把它设置为你的模式:
{ type: Number, min: 100000000, max: 999999999 }
看看mongoose的文件: mongoose文件