Reputation: 430
I am using mongodb in connection with mongoose in node.js. I have following simple collection schema:
var calEventSchema = mongoose.Schema({
"_userId" : { type: mongoose.Schema.Types.ObjectId, ref: 'PanelUser' },
"task" : String,
"startTime" : Date,
"endTime" : Date
});
exports.CalEvent = mongoose.model('CalEvent', calEventSchema);
Now I want to add a 'category' property to the CalEvent so that the data looks something like this:
{
"_id": "AAA",
"_userId": "BBB",
"task": "Meeting with Bob",
"startTime": "2016-04-12T06:00:00.000Z",
"endTime": "2016-04-12T08:30:00.000Z",
"category" : "meeting"
}
The options I have for the category might be ['meeting', 'vacations', 'civil service', 'offsite']. When the user creates an event, he should get all the category options to chose from (e.g. dropdown). Even more: a power user should be able to extend the categories.
I thought about some ways to do it but I am not happy with either:
Is there an better way to do this ?
Upvotes: 0
Views: 1107
Reputation: 16805
Normally you can solve this problem using enum
if fixed enum values.
then your model would be like:
var calEventSchema = mongoose.Schema({
"_userId" : { type: mongoose.Schema.Types.ObjectId, ref: 'PanelUser' },
"task" : String,
"startTime" : Date,
"endTime" : Date,
"category:{
type: String,
default: 'meeting', // if you want to set as default value
enum:['meeting', 'vacations', 'civil service', 'offsite']
}
});
exports.CalEvent = mongoose.model('CalEvent', calEventSchema);
But if you want to extend or reduce category
options dynamically, then better create another model for category
and refer the selected category like _userId
Upvotes: 2