'MongoDB - aggregation - Update after lookup and match
I have a model Product and a model Event. Each product has an Event associated, and each Event has a date. Also each product has a virtual property called availability
I need to set availability to false, when the input date matches the date in Event.
So far I have filter all the products that matches the given date like this:
exports.checkDateAvailability = function (req, res) {
const dateAvailability = moment(req.query.dateAvailability).format("DD-MM-YYYY")
Product.aggregate([
{
$lookup: { from: Event.collection.name, localField: 'event', foreignField: '_id', as: 'event' }
},
{
$unwind: '$event',
},
{
$addFields: { date: { $dateToString: { date: '$event.date', format: "%d-%m-%Y" } } }
},
{
$match: { date: dateAvailability },
},
]).then(response => { res.send(response) })
}
Now I want to update those products availability property to false. I know that from version 4.2 it is available the update command, but I failed implementing it after the $match. Any idea on how can I proceed?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
