Home > Software engineering >  Is it possible to define the range of integer values in a Number field in MongoDB?
Is it possible to define the range of integer values in a Number field in MongoDB?

Time:03-28

      slider_value: {
        type: Number,
        required: false,
      },

This is the Mongoose schema for one of the fields in my MongoDB model.

Is it possible to specify the acceptable integer values in this field?

for example, this field may only accept the integers from 1 to 10.

CodePudding user response:

There are min and max validators, and you should also check for isInteger

slider_value: {
  type: Number,
  required: false,
  min: 1,
  max: 10,
  validate : {
    validator : Number.isInteger,
    message   : '{VALUE} is not an integer value'
  }
},

CodePudding user response:

You are looking for min and max validators:

slider_value: {
  type: Number,
  required: false,
  min: 1,
  max: 10
},
  • Related