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

207
Vistas
how to create document using nested mongodb schema?

I'm trying to create a new document using javascript for mongoDB database, and add values to deeply nested properties in the schema. Specifically i'm trying to add values to the taskItem, highlight, and identifier properties on task. Right now my schema looks like this

const todoTaskSchema = new mongoose.Schema({
  username: String,
  password: String,
  task: [
      {
              taskItem: String,
              highlight: {type: Boolean, default: false},
              identifier: Number,
      
      }],
  subTask: [
      {
          pointer: Number,
          tasks: Array,
      },
    ]
})

My destructured properties look like this.

const { username, password, task: [ { taskItem: taskName, highlight: starred, identifier} ] } = req.body

I'm using the .create method to create the document. It's argument looks like this:

dbModel.create({ taskName, starred, identifier })

If I do this, and make a post request from postman using something like

  {
  "username": "Bruce Lee",
  "password": "enter the dragon",
  "task": [
      {
          "taskItem": { 
              "type": "figure out error",
              "highlight": true,
              "identifier": 1
               },
          "subTask": [
              {
                  "subTaskItem": { 
                      "pointer": 1,
                      "type": "abc", 
                      "tasks": [1, 2, 3] }
              }]
        }]
    }

the output on mongoDb is:

_id: 62ba2300c854b603578af1ac
task:Array
subTask:
Array
__v:0

can anyone tell me how to access the correct properties in the schema in order to correctly fill out the document using .create()?

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