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

Viewed 125
      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.

2 Answers

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'
  }
},

You are looking for min and max validators:

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