Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

291
Vistas
Can a subdocument be required in mongoose?

Is it possible to have nested schemas in mongoose and have a required validator on the children? Something like this:

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  }
});

const eventSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  host: {
    type: userSchema,
    required: true
  }
});

I can't find anything in the documentation. Thanks.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Yes, your schema is correct.

The docs for mongoose nested schema (SubDocuments) can be found here

over 4 years ago · Santiago Trujillo Denunciar

0

i suppose you'll update eventSchema with subdocuments of type user model. you can use { runValidators: true} for update.

eventModel.update({ name: 'YOUR NAME' }, { $push: { host: user } }, { runValidators: true}, function(err) {

})
over 4 years ago · Santiago Trujillo Denunciar

0

You can use the nested schema in mongoose.

It will also give you he Object Id on each sub schema values as well.

  • Doc: Here
  • Example: Here
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda