Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to access enumValues in mongoose from a Model or Document
var mongoose = require('./index')
, TempSchema = new mongoose.Schema({
salutation: {type: String, enum: ['Mr.', 'Mrs.', 'Ms.']}
});
var Temp = mongoose.model('Temp', TempSchema);
console.log(Temp.schema.path('salutation').enumValues);
var temp = new Temp();
console.log(temp.schema.path('salutation').enumValues);
@wavded

This comment has been minimized.

Show comment Hide comment
@wavded

wavded May 3, 2011

thanks!

wavded commented May 3, 2011

thanks!

@renatoargh

This comment has been minimized.

Show comment Hide comment
@renatoargh

renatoargh Nov 14, 2012

thanks!!! :)

thanks!!! :)

@kirrg001

This comment has been minimized.

Show comment Hide comment
@kirrg001

kirrg001 Jun 11, 2013

great!

great!

@atishn

This comment has been minimized.

Show comment Hide comment
@atishn

atishn Jul 24, 2013

Thanks

atishn commented Jul 24, 2013

Thanks

@chichilatte

This comment has been minimized.

Show comment Hide comment
@chichilatte

chichilatte Oct 21, 2013

So neat, but doesn't work for me (mongoose 3.6.20): for the path() function, my enum fields must be prefixed with ".enum", and there is no "enumValues" property on the returned object. This works for me... console.log(Temp.schema.path('salutation.enum').options.type) // We get ['Mr.', 'Mrs.', 'Ms.']

Update: bnoguchi's neater version works if you do Temp.path("salutation").enum('Mr.', 'Mrs.', 'Ms.'); after you've defined the schema (i was using salutation: {enum: ['Mr.', 'Mrs.', 'Ms.']} in the Schema constructor parameter, which doesn't seem to work as well).

So neat, but doesn't work for me (mongoose 3.6.20): for the path() function, my enum fields must be prefixed with ".enum", and there is no "enumValues" property on the returned object. This works for me... console.log(Temp.schema.path('salutation.enum').options.type) // We get ['Mr.', 'Mrs.', 'Ms.']

Update: bnoguchi's neater version works if you do Temp.path("salutation").enum('Mr.', 'Mrs.', 'Ms.'); after you've defined the schema (i was using salutation: {enum: ['Mr.', 'Mrs.', 'Ms.']} in the Schema constructor parameter, which doesn't seem to work as well).

@zubairalam

This comment has been minimized.

Show comment Hide comment
@zubairalam

zubairalam Nov 21, 2014

I've salutation sub-document inside profile document, so Temp.schema.path('profile.salutation').enumValues works in my case.

I've salutation sub-document inside profile document, so Temp.schema.path('profile.salutation').enumValues works in my case.

@m4grio

This comment has been minimized.

Show comment Hide comment
@m4grio

m4grio Jan 6, 2015

Thanks!

m4grio commented Jan 6, 2015

Thanks!

@aboutvale

This comment has been minimized.

Show comment Hide comment
@aboutvale

aboutvale Apr 26, 2015

Great!!

Great!!

@michaelezehi

This comment has been minimized.

Show comment Hide comment
@michaelezehi

michaelezehi Jan 19, 2016

This was very helpful. Thanks

This was very helpful. Thanks

@renanborgez

This comment has been minimized.

Show comment Hide comment
@renanborgez

renanborgez Mar 8, 2016

Thanks a lot, helpful!!

Thanks a lot, helpful!!

@LadwaAditya

This comment has been minimized.

Show comment Hide comment
@LadwaAditya

LadwaAditya Apr 19, 2016

Thank you

Thank you

@manouman

This comment has been minimized.

Show comment Hide comment
@manouman

manouman May 24, 2016

If you want to know all the enum values inside in array type, you need something like this:

temp.path('salutation.0.type').enumValues

If you want to know all the enum values inside in array type, you need something like this:

temp.path('salutation.0.type').enumValues

@zamrokk

This comment has been minimized.

Show comment Hide comment
@zamrokk

zamrokk Sep 7, 2016

👍 This kind of thing is possible too :

collaboration.schema.path('typeRef').enumValues[0]

zamrokk commented Sep 7, 2016

👍 This kind of thing is possible too :

collaboration.schema.path('typeRef').enumValues[0]

@chipro

This comment has been minimized.

Show comment Hide comment
@chipro

chipro Jan 15, 2017

Wow, I was just wondering how to do this as I need the enum data to implement a droplist. Very helpful, thanks!

chipro commented Jan 15, 2017

Wow, I was just wondering how to do this as I need the enum data to implement a droplist. Very helpful, thanks!

@HasanSa

This comment has been minimized.

Show comment Hide comment
@HasanSa

HasanSa Mar 29, 2017

Thanks

HasanSa commented Mar 29, 2017

Thanks

@LukeXF

This comment has been minimized.

Show comment Hide comment
@LukeXF

LukeXF Nov 5, 2017

Perfect, thank you!

LukeXF commented Nov 5, 2017

Perfect, thank you!

@jeffwesson

This comment has been minimized.

Show comment Hide comment
@jeffwesson

jeffwesson Dec 22, 2017

Thank you!

Thank you!

@imromec

This comment has been minimized.

Show comment Hide comment
@imromec

imromec Apr 2, 2018

How to access the enum from a non-model schema? Like if I am using a discriminator schema and in that schema, there is enum field, how can I can access that?

imromec commented Apr 2, 2018

How to access the enum from a non-model schema? Like if I am using a discriminator schema and in that schema, there is enum field, how can I can access that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment