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
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 Respuestas
Responde la pregunta

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 Denunciar

0

Try this:

commands: [
{ type: mongoose.Schema.Types.ObjectId }
]
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