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

137
Vistas
Mongoose FindOneAndUpdate not Updating when Pushing an Object to a Nested Array

When trying to push to a nested array it's not updating in my database with the following code:

const obj = {
      key: key,
      user: user,
      description: description,
      date: Date.now(),
      guildId: guildId,
    };

    const guild = await this.GuildModel.findOneAndUpdate(
      { guildId: guildId },
      { $push: { 'guildData.commandLogs': obj } },
    );

My schema:

const GuildSchema = {
  guildId: { type: String },
 
  guildData: {
    commandLogs: [CommandLogsSchema],
  },

Any idea why my database isn't updating?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think you might have some typos in your model:

 const mongoose = require('mongoose'); 
 const Schema = mongoose.Schema;
 
 const GuildSchema = new Schema({   guildId: { type: String },  
 guildData: {
    commandLogs: [CommandLogsSchema],   })

in your controller, the filter condition that you're passing is not clear, which guildId you are using? also when you call the guildData.commandLogs field to update it you are missing brackets

const filter = { guildId: heretheactualId };
const update = {  { $push: { commandLogs: obj} }};

  const guild = await this.GuildModel.findOneAndUpdate(
       filter,
       update,
    );

this is not going to fix your problem because I don't know what kind of errors you're having or your database model but these tips can already help.

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