How to Create and Use Enum in Mongoose

node.jsMongodbExpressEnums

node.js Problem Overview


I am trying to create and use an enum type in Mongoose. I checked it out, but I'm not getting the proper result. I'm using enum in my program as follows:

My schema is:

var RequirementSchema = new mongooseSchema({
   status: {
        type: String,
        enum : ['NEW','STATUS'],
        default: 'NEW'
    },
})

But I am little bit confused here, how can I put the value of an enum like in Java NEW("new"). How can I save an enum in to the database according to it's enumerable values. I am using it in express node.js.

Thanks

node.js Solutions


Solution 1 - node.js

The enums here are basically String objects. Change the enum line to enum: ['NEW', 'STATUS'] instead. You have a typo there with your quotation marks.

Solution 2 - node.js

From the docs

Mongoose has several inbuilt validators. Strings have enum as one of the validators. So enum creates a validator and checks if the value is given in an array. E.g:

var userSchema = new mongooseSchema({
   userType: {
        type: String,
        enum : ['user','admin'],
        default: 'user'
    },
})

Solution 3 - node.js

Let say we have a enum Role defined by

export enum Role {
  ADMIN = 'ADMIN',
  USER = 'USER'
}

We can use it as type like:

{
    type: String,
    enum: Role,
    default: Role.USER,
}

Solution 4 - node.js

If you would like to use TypeScript enum you can use it in interface IUserSchema but in Schema you have to use array (Object.values(userRole)).

enum userRole {
    admin = 'admin',
    user = 'user'
}

interface IUserSchema extends Document {
    userType: userRole
}

const UserSchema: Schema = new Schema({
    userType: {
        type: String,
        enum: Object.values(userRole),
        default: userRole.user, 
        required: true
    }
});

Solution 5 - node.js

Enums is String objects so for example : enum :['a','b','c'] or probably like this const listOfEn = ['a','b','c']; => enum: listOfEn

Solution 6 - node.js

In a Schema design, you can easily add an enum value by using enum keyword like this: -

catagory: {
    type: String,
    enum: ['freeToPlay','earlyAccess','action','adventure','casual','indie','massivelyMultiplayer','racing','simulation','RPG','sports','statigy'],
    default: 'freeToPlay'
},

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrabjot SinghView Question on Stackoverflow
Solution 1 - node.jsMindstormer619View Answer on Stackoverflow
Solution 2 - node.jsDeeksha SharmaView Answer on Stackoverflow
Solution 3 - node.jsMohammed Al-ReaiView Answer on Stackoverflow
Solution 4 - node.jsJax-pView Answer on Stackoverflow
Solution 5 - node.jshien711View Answer on Stackoverflow
Solution 6 - node.jsDiganta ChaudhuriView Answer on Stackoverflow