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

212
Visualizações
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 à 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