Search code examples
mongodbmongoosemongoose-schema

Array of default values


I have a schema looks like this

const userSchema = mongoose.Schema({
    id: {
        type: String,
    },
    name: {
        type: String,
        required: true,
    },
    email: {
        type: String,
        required: true,
        unique: true,
    },
    password: {
        type: String,
        required: true,
    },
    notes: [
        {
            type: mongoose.Types.ObjectId,
            ref: 'Note',
        },
    ],
    folders: [
        {
            type: String,
            default: ['My notes', 'Todos', 'Projects', 'Journal', 'Reading list'],
        },
    ],
});

here folder is an array, and I want to add some value by default when I create a user. But it doesn't work. It always returns an empty array. I want it to be like this by default,

folders: ['My notes', 'Todos', 'Projects', 'Journal', 'Reading list']

How can I achieve that?


Solution

  • Try this

    folders: {
      type: [String],
      default: ['My notes', 'Todos', 'Projects', 'Journal', 'Reading list']
    }