To create and use enum in Mongoose and JavaScript, we can add an enum field into the schema.
For instance, we write
const userSchema = new mongoose.Schema({
userType: {
type: String,
enum: ["user", "admin"],
default: "user",
},
});
to create a schema with Schema
.
We call it with an object that has the enum
property with the possible values for the enum to create the userType
enum field that is a string that can either be 'user'
or 'admin'
.