I have a schema like this:
var CustomUserSchema = new Schema({
role: [],
permissions: [],
});
permissions
field store an array of strings that looks like this:
["Delete", "Show","Create"]
whereas
role
field stores an array of objects that looks like this:
[
{
name:"admin",
priority:10,
permissions: ["Delete", "Show" , "update"]
},
{
name:"user",
priority:5,
permissions: ["Delete", "Show"]
}
]
Now, my requirement is to be able to store "Show" as default value for permissions
field in the schema and to store 'user' as default value for name inside of role field , priority 0 for priority
inside of role
field and 'Show' for permissions
inside of role
field.
Trying myself, I came up with this:
var CustomUserSchema = new Schema({
role: [{
name: {type: String, default: 'user'},
priority:{ type: Number, default: 0 } ,
permissions: [{type:String, default:'Show'}]
}],
permissions: [{type:String, default:'Show'}]
});
But it does not assign the default values to the fields at all and gives an array of size 0 to the fields .
What seems wrong with above schema? How do I store these as default values?
See Question&Answers more detail:os