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

206
Views
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
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!