Skip to content

Instantly share code, notes, and snippets.

Embed
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
Copy link

wavded commented May 3, 2011

thanks!

@renatoargh
Copy link

renatoargh commented Nov 14, 2012

thanks!!! :)

@kirrg001
Copy link

kirrg001 commented Jun 11, 2013

great!

@atishn
Copy link

atishn commented Jul 24, 2013

Thanks

@chichilatte
Copy link

chichilatte commented 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).

@zubairalam
Copy link

zubairalam commented Nov 21, 2014

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

@m4grio
Copy link

m4grio commented Jan 6, 2015

Thanks!

@aboutvale
Copy link

aboutvale commented Apr 26, 2015

Great!!

@michaelezehi
Copy link

michaelezehi commented Jan 19, 2016

This was very helpful. Thanks

@renanborgez
Copy link

renanborgez commented Mar 8, 2016

Thanks a lot, helpful!!

@adityaladwa
Copy link

adityaladwa commented Apr 19, 2016

Thank you

@manouman
Copy link

manouman commented 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

@zamrokk
Copy link

zamrokk commented Sep 7, 2016

👍 This kind of thing is possible too :

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

@chipro
Copy link

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
Copy link

hasansa commented Mar 29, 2017

Thanks

@LukeXF
Copy link

LukeXF commented Nov 5, 2017

Perfect, thank you!

@jeffwesson
Copy link

jeffwesson commented Dec 22, 2017

Thank you!

@imromec
Copy link

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?

Copy link

ghost commented Dec 3, 2018

nice!

@AlfredBryan
Copy link

AlfredBryan commented Jan 10, 2019

nice

@DennyTim
Copy link

DennyTim commented May 23, 2019

thanks.

@iamromec
Copy link

iamromec commented Jun 28, 2020

To find enums of all enum properties:

const schema = Temp.schema;
const enums = Object.keys(schema.obj)
        .filter(k => schema.obj[k].hasOwnProperty('enum'))
        .map(k => {
          let o = {};
          o[k] = schema.obj[k].enum;
          return o;
        })
        .reduce((r, v) => Object.assign(r, v), {});

@utsukushiihime
Copy link

utsukushiihime commented Sep 22, 2020

Just what I needed. Thank you!

@FrankDev-327
Copy link

FrankDev-327 commented May 25, 2021

for me it worked like this:

//model typeContacts.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const arrayTypeContacts = [
{ type: 'phone', name: 'principal', icon: 'phone' },
{ type: 'email', name: 'principal', icon: 'envelope' },
{ type: 'whatsapp', name: 'principal', icon: 'whatsapp' },
];
const TypeConctactSchema = Schema({
contactType: {
type: String,
enum: arrayTypeContacts,
default: arrayTypeContacts
}
});

const TypeContacts = mongoose.model('typecontacts', TypeConctactSchema);
module.exports = TypeContacts;

//controller
const data = await TypeContactModel.schema.path('contactType').options.enum;

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