Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

414
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda