How to Create and Use Enum in Mongoose

Prabjot Singh picture Prabjot Singh · Mar 27, 2015 · Viewed 85.9k times · Source

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.

Answer

Mindstormer619 picture Mindstormer619 · Jul 2, 2015

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.