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?