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

125
Vistas
MongoDB Create New Document that references another Collection's Document IDs

I am trying to create a new document in the Form collection. This document references many FormSection documents. Here are the Schemas:

const FormSchema = new Schema({ 
    title: {
        type: String,
        required: true,
        unique: true
    },
    description: {
        type: String,
        required: true,
        unique: true
    },
    sections: [{
        type: FormSectionDetails
    }],
    createdDate: {
        type: String,
        required: false,
        unique: true
    },
    lastEdited: {
        type: String,
        required: false,
        unique: true
    }
});

const FormSectionDetails = new Schema({
    section: {
        type: Schema.Types.ObjectId,
        ref: 'FormSection',
        required: true
    },
    position: {
        type: Number,
        required: true
    }
});

const FormSectionSchema = new Schema({ 
    name: {
        type: String,
        required: true,
        unique: true
    },
    display: {
        type: String,
        required: true,
    },
    category: {
        type: String,
        required: true
    },
...
});

let FormSection;
try {
    FormSection = mongoose.connection.model('FormSection');
} catch (e) {
    FormSection = mongoose.model('FormSection', FormSectionSchema);
}

However, when I try to add a new document to the Forms collection, I get an error:

Document being inserted:

formData = {
    "title": "Returning Members",
    "description": "Returning Members",
    "sections": 
    [{
        "section": "6292c0fbd4837faca1d85d4d",
        "position": 1
    },
  {
        "section": "6292c0fbd4837faca1d85d4e",
        "position": 2
    },
...
}

Code being run:

formdata.sections.map(s => {
   return {
       ...s,
       section: ObjectId(s.section),
   }}
);
return await FormSection.create(formdata);

Error message:

ValidationError: category: Path `category` is required., display: Path `display` is required.````

Seems like it is trying to create a new FormSection document. I don't want it to create a new FormSection document. I just want it to reference existing FormSection documents using the Object IDs I specified.
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The Issue seems to be with how you declare the section field in the FormSchema. Try this:

const FormSchema = new Schema({
  title: {
    type: String,
    required: true,
    unique: true
  },
  description: {
    type: String,
    required: true,
    unique: true
  },
  sections: [{
    type: ObjectId,
    ref: 'FormSectionDetails',
    required: true,
  }],
  createdDate: {
    type: String,
    required: false,
    unique: true
  },
  lastEdited: {
    type: String,
    required: false,
    unique: true
  }
});

This would just store the _ids of the existing FormSectionDetails

about 4 years ago · Juan Pablo Isaza Denunciar

0

It turns out I was inserting the document into the wrong collection. Instead of the code snippet:

return await FormSection.create(formdata);

It should actually be:

return await Form.create(formdata);

The error message should have been a more obvious hint for me as to what the problem was.

about 4 years ago · Juan Pablo Isaza 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