2017-10-18 63 views
2

我有这个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为我获得下一个验证错误:

{ 
    "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" 
} 

是否有任何其他方式来验证Number类型字段的准确长度? 还是我误解了猫鼬店的数字?

回答

2

minmax意味着允许的数字量提供的Number,因为错误说:

(320981350) is more than maximum allowed value (9)

他们的意思是该领域的实际最小值/最大值与Number类型,因此,例如在

{ 
    type: Number, 
    min : 101, 
    max : 999 
} 
  • 最大Number所有欠是999
  • 允许的最小Number101

在你的情况,如果你有9位数字作为您id,在架构定义如下领域:

{ 
    type: Number, 
    min : 100000000, 
    max : 999999999 
} 
0

minmax并不意味着min-lengthmax-length ...

所以要达到您的目标,您最好将此设置为哟UR模式:

{ 
    type: Number, 
    min: 100000000, 
    max: 999999999 
} 

看看猫鼬文档: mongoose Doc