Enums in Mongoose

May 23, 2022

Mongoose String and Number types have an enum validator. The enum validator is an array that will check if the value given is an item in the array. If the value is not in the array, Mongoose will throw a ValidationError when you try to save().

const testSchema = new mongoose.Schema({
  status: {
    type: String,
    enum: ['valid', 'invalid']
  }
})

const Test = mongoose.model('Test', testSchema);

await Test.create({ name: 'not a status' }); // throws validation error
await Test.create({ name: 'valid' }); // works

Typescript enums

You can also use Typescript Enums. At runtime, TypeScript enums are just POJOs where the object's values are the enum values. When you set enum to an object, Mongoose will run Object.values() on the object to get the desired values.

enum Status {
  Valid,
  Invalid
};

const testSchema = new mongoose.Schema({
  rating: {
    type: Number,
    enum: Rating
  xw}
});

const Test = mongoose.model('Test', testSchema);

await Test.create({ name: 'invalid' }); // throws validation error
await Test.create({ name: 'Valid' }); // works

Want to become your team's MongoDB expert? "Mastering Mongoose" distills 8 years of hard-earned lessons building Mongoose apps at scale into 153 pages. That means you can learn what you need to know to build production-ready full-stack apps with Node.js and MongoDB in a few days. Get your copy!

More Mongoose Tutorials