Mongoose schema field from list of strings (enum)

In the following example we define template attribute to be of type String with values defined in the enum array:

const noteSchema = new Schema({
    title: {type:String, required: true},
    type: {type:String, required: true, default: 'note'},
    content: String,
    reference: String,
    tags: [String],
    template: {type:String, enum: ['note', 'checklist']},
    userId: {type: String, ref:'User'},
    __v: { type: Number, select: false}
},
{
  timestamps: true
});

Shared with from Codever. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Codever is open source on Github ⭐🙏

Subscribe to our newsletter for more code resources and news

Adrian Matei (aka adixchen)

Adrian Matei (aka adixchen)
Life force expressing itself as a coding capable human being

routerLink with query params in Angular html template

routerLink with query params in Angular html template code snippet Continue reading