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

207
Vistas
Update subdocument array in mongoose *after* querying for objectId

I'm trying to find a particular objectId in my subdocument and update the 'attended' field.

I cannot figure out how to query into the nested array then push the update. Here's my model:

const eventSchema = new Schema({
  address: String,
  description: String,
  guests: [
    {
      phone: Number,
      attended: String // this should be the "Y" from the server
    }
  ],
});

Here's my function:

module.exports.checkIn = async(req, res) => {
    Event.findByIdAndUpdate(req.body.guest, { 'guests.attended': "Y" }, function(err, post) {
        if (err) {
          res.send(err);
        } else {
          res.send(post);
        }
      });
    };

My understanding is this is only querying into the Event model and not down into the guest array. My client side doesn't by-default know which guest is being updated but it's sending that data in a variable called 'guest' (guest is a string that matches one of the objectId's in the array).

Everything I'm seeing on stackoverflow or in the docs/on youtube is structured for functions that can push the default req.body.guest onto the model. I first have to query and find that based on my guest variable, then update. This is the part that is throwing me off. How do I add that into my function?

about 4 years ago · Juan Pablo Isaza
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