Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose Model Options field

Hi I just started playing with Mongoose. It seems pretty awesome!

Now coming from a Django background, how would one implement a type of options field like:

STATUS_OPTIONS : [{"Open",1},{"Closed",2},{"Pending",3"}]
status: { type:String, required:true, options:STATUS_OPTIONS },

So that it can be set like status = Open or something like that.

Or should this just be a normal String field and I set it accordingly in my app?

like image 858
Harry Avatar asked Dec 27 '22 08:12

Harry


1 Answers

You can constrain a Mongoose schema string field to a set of enumeration values with the enum attribute:

var s = new Schema({
    status: { type: String, enum: ['Open', 'Closed', 'Pending'] }
});
like image 69
JohnnyHK Avatar answered Dec 28 '22 23:12

JohnnyHK