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

130
Views
Save array of ObjectId's using Mongoose's Schema

I have a Mongoose Schema like:

const Role = new Schema({
    guildID: {
        type: Schema.Types.ObjectId,
        ref: 'guilds',
        required: true
    },
    roles: {
        owner: {
            id: {
                type: Number,
                required: false
            },
            commands: [[Schema.Types.ObjectId]]
        }
    }
})

And a small function to test whether it saves the data as desired which contains:

const roleTest = new Role({
        guildID: "61a679e18d84bff40c2f88fd",
        roles: {
            owner: {
                id: 123456789
            },
            commands: [
                "61af57d828b9fd5a07dbdcba",
                "61af5a6728b9fd5a07dbdcbb",
                "61af5ab728b9fd5a07dbdcbc"
            ]
        }
    })

    roleTest.save((err, doc) => {
        if (err) return res.sendStatus(500)
        console.log('Done')
    })

It saves everything correctly except the array ObjectIds (commands). What is going wrong here?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You've written the commands in schema with nested array. Try with single array:

{
commands: [Schema.Types.ObjectId],
}
about 4 years ago · Juan Pablo Isaza Report

0

Try this:

commands: [
{ type: mongoose.Schema.Types.ObjectId }
]
about 4 years ago · Juan Pablo Isaza Report
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!