Express JS How to push object to an array of child object
You can do it using $push.
I think this code may help you !
router.patch('/:slug/:floorSlug/add-location', (req, res) => {
const slug = req.params.slug;
const { floorSlug } = req.body;
const querySlug = '^' + slug + '$';
const locations = req.body.locations;
Map.findOneAndUpdate(
{
$and: [
{ "slug": { '$regex': querySlug, $options: 'i' } },
{ 'floors.floorSlug': floorSlug }]
},
{
$push: { 'floors.$.locations': locations }
}
);
});