risuch
risuch

Reputation: 430

mongodb/mongoose: How to model extendable enum-like data

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:

  1. Create a separate collection to just store the CalEvents' Categories (But how to make sure the CalEvent schema considers just the values listed in the category collection?)
  2. Do it the Relational way: Create own collection like in 1., then ref the category with the _id to a field _catId in CalEvents, this seems somehow exactly like I would do it with relational tables and feels somehow wrong.

Is there an better way to do this ?

Upvotes: 0

Views: 1107

Answers (1)

Shaishab Roy
Shaishab Roy

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

Related Questions