Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!