Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

      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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'
  }
},
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading