Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

413
Views
how to configure default enum value in mongoose sub doc?

I am using mongodb along with mongoose js in my node js application . I created a mongoose document schema called “CompanySchema” which is using “TeamSchema” (another mongoose document schema) as a sub document. Inside this “TeamSchema” it has an array defined as employees which is using “EmployeeSchema” (another mongoose document) as a subdocument. So my question is when I am trying to save the document “CompanySchema” the default value for requirement status “unmet” is not getting set. So can you guys explain me what I am doing wrong in here?

export var EmployeeSchema = new Schema({
 id: {
   type: String
 },
 requirement: {
   type: {
     status: {
       type: String,
       enum: ['met' 'unmet'],
       default : 'unmet'
     }
   },
   default: null
 },
});

export var TeamSchema = mongoose.model<TeamModel>("Team", new mongoose.Schema({
 id: {
   type: String,
 },
 name: {
   type: String
 },
 employees: [EmployeeSchema]
}));

export var CompanySchema = mongoose.model<CompanyModel>("Company", new mongoose.Schema({
 id: {
   type: String
 },
 team: TeamSchema.schema,
}));
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think there is two problems in your schema.

First, you use the Mongoose reserved keyword type.

By default, if you have an object with key 'type' in your schema, mongoose will interpret it as a type declaration.

Mongoose doc: typeKey

Second, you set a default value to null, which should give you an error if you had not used the type keyword as a property name. Try to rename type to requirement_type for exemple and you'll get this error:

TypeError: Invalid value for schema path `requirement.default`

Which is coherent since it precisely needs a type to set default value.

SchemaType#default(val)
Sets a default value for this SchemaType.

Mongoose doc: SchemaType-default

I do not really understand why you want to make it null by default, but you can do it by adding for exemple a Mixed type:

requirement: {
   type: {},
   requirement_type: {
     status: {
       type: String,
       enum: ['met', 'unmet'],
       default : 'unmet'
     }
   },
   default: null
 }
 // => { requirement_type: null }

Or you can remove the default and you'll get:

requirement: {
   requirement_type: {
     status: {
       type: String,
       enum: ['met', 'unmet'],
       default : 'unmet'
     }
   }
 }
// => { requirement_type: { status: 'unmet' } }

Note: You must separate the enum values ​​with a comma.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!