My main schema is an event, with some steps embedded in an array. I want to query the events, and filter the steps it has with some conditions. So the event is returned with not all the steps it has, with just the ones that match the condition. I can do it using aggregation, but I need the documents returned as mongoose documents so I can use the save() function on each, so it has to be done using just mongoose I suppose.
a sample event:
{
"_id" : ObjectId("5d57b6ad754e9d1ec0c123e6"),
"title" : "wedding",
"user" : ObjectId("5d4fedfd63dc55207cf128ff"),
"date" : ISODate("2019-09-24T20:00:00.000Z"),
"steps" : [
{
"_id" : ObjectId("5d57b6ad754e9d1ec0c123e7"),
"title" : "home",
"startDate" : ISODate("2019-06-27T20:00:00.000Z"),
"endDate" : ISODate("2019-07-21T20:00:00.000Z"),
"isDone" : false,
"state" : 2
},
{
"_id" : ObjectId("5d57b6ad754e9d1ec0c123e8"),
"title" : "guests",
"startDate" : ISODate("2019-07-07T20:00:00.000Z"),
"endDate" : ISODate("2019-07-30T20:00:00.000Z"),
"isDone" : false,
"state" : 2
},
{
"_id" : ObjectId("5d57b6ad754e9d1ec0c123e9"),
"title" : "car",
"startDate" : ISODate("2019-07-11T20:00:00.000Z"),
"endDate" : ISODate("2019-07-20T20:00:00.000Z"),
"isDone" : false,
"state" : 2
}
]
};
the aggregation I have so far:
const now = new Date();
Event.aggregate([
{
$match: { date: { $gte: now } },
},
{
$unwind: '$steps',
},
{
$match: { 'steps.startDate': { $lte: now }, 'steps.endDate': { $gte: now }, 'steps.state': { $ne: 1 } },
},
])
I have searched the docs but I did not find anything.