Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the enum values from a mongoose schema?

Tags:

mongoose

This is my scheme:

  var documentSchema = mongoose.Schema({
    'facts': [{
      'type': { type: String, required: true, uppercase: true, enum: ['TEXT'], default: 'TEXT'},
      'label': { type: String },
      'value': { type: String }
    }],

    'type': { type: String, required: true, uppercase: true, enum: ['TEXT', 'MARKDOWN', 'JSON'], default: 'TEXT'},

    'lastModified': { type: Date, required: true, default: Date.now },
    'created': { type: Date, required: true, default: Date.now }
  }, {
    versionKey: 'version'
  });
  var DocumentModel = mongoose.model('Document', documentSchema);

I can get enums with:

DocumentModel.schema.path('type').enumValues,

But not with:

DocumentModel.schema.path('facts.type').enumValues,

I get TypeError: Cannot read property 'enumValues' of undefined. What am I doing wrong?

like image 496
Justin808 Avatar asked Mar 07 '14 05:03

Justin808


People also ask

What is enum in Mongoose schema?

Mongoose has several inbuilt validators. Strings have enum as one of the validators. So enum creates a validator and checks if the value is given in an array. E.g: const userSchema = new mongoose. Schema({ userType: { type: String, enum : ['user','admin'], default: 'user' }, })

What does REF do in Mongoose schema?

The ref option is what tells Mongoose which model to use during population, in our case the Story model. All _id s we store here must be document _id s from the Story model. Note: ObjectId , Number , String , and Buffer are valid for use as refs.

What does Mongoose model return?

mongoose. model() returns a Model ( It is a constructor, compiled from Schema definitions).

What is the difference between schema and model in Mongoose?

Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

How to create and use enums in mongoose?

Sometimes, we want to create and use enums in Mongoose. In this article, we’ll look at how to create and use enums in Mongoose. How to create and use enums in Mongoose? To create and use enums in Mongoose, we can set the enum property when we create our schema. to create the UserSchema with the userType column.

What is a schema in mongoose?

A Mongoose ‘schema’ is a document data structure (or shape of the document) that is enforced via the application layer. ‘Models’ are higher-order constructors that take a schema and create an instance of a document equivalent to records in a relational database.

Does mongoose support TypeScript enums?

Also, it's worth noting that mongoose currently does not support TypeScript, we're planning to have official TypeScript definitions in version 5.11. Mongoose doesn't have built-in support for TypeScript enums, and it really can't because TypeScript enums are just POJOs at runtime.

How to get enum possible values in a MySQL database?

You can get the enum possible values in a MySQL database with the help of INFORMATION_SCHEMA.COLUMNS table. The syntax is as follows − To understand the above syntax, let us create a table with an ENUM data type.


1 Answers

Since facts is an array with an embedded schema you will need to access that schema first and then you can get the enum values in the same manner you did with type.

DocumentModel.schema.path('facts').schema.path('type').enumValues
like image 118
Jason Cust Avatar answered Oct 15 '22 22:10

Jason Cust