169

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Prabjot Singh
  • 4,491
  • 8
  • 31
  • 51
  • 22
    This question has a ton of revisions fixing OP's typo and thus fixing the problem, which made the accepted answer a bit confusing. please consider the original array written was missing a "'": `enum: ['NEW, 'STATUS']` – Alvaro Carvalho Sep 23 '21 at 19:24

6 Answers6

203

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.

Mindstormer619
  • 2,706
  • 1
  • 16
  • 16
  • how do you link this to the user table? mine doesn't work. my user table i inserted this one role: { type: mongoose.Schema.Types.ObjectId, ref: 'roles', }, – Grizzly Bear Sep 29 '20 at 08:05
  • To be precise, what you're presenting here is an Array of Strings, not a "String object". – matewka Dec 06 '21 at 21:45
133

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:

const userSchema = new mongoose.Schema({
   userType: {
        type: String,
        enum : ['user','admin'],
        default: 'user'
    },
})

Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
Deeksha Sharma
  • 3,199
  • 1
  • 19
  • 16
  • Thanks it works for me to store default userType. How to change userType to amdin from js? – Qui-Gon Jinn Jul 12 '20 at 02:39
  • how do you link this to the user table? mine doesn't work. my user table i inserted this one role: { type: mongoose.Schema.Types.ObjectId, ref: 'roles', }, – Grizzly Bear Sep 29 '20 at 08:05
45

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,
}
Striped
  • 2,544
  • 3
  • 25
  • 31
Mohammed Al-Reai
  • 2,344
  • 14
  • 18
18

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
    }
});
Jax-p
  • 7,225
  • 4
  • 28
  • 58
10

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

hien711
  • 99
  • 2
  • 3
6

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'
},
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 14 '21 at 15:31