I am trying to return just the array from a deep nested object. I have a top schema year, with 12 embedded months, and each month has an array of reference id’s for envelopes. I can select the specific month using this
/**
* Get all envelopes of a month of a year
*/
yearRouter.get("/:year/:month/all", async (req, res) => {
try {
const { year, month } = req.params;
const projection = {
months: { $elemMatch: { month: month }},
};
const data = await Year.findOne({ year: year }, projection);
res.json(data);
} catch (e) {
res.status(500).json({ message: e.message });
}
});
and that gets me
{
"_id": "635997a3a7099a37783cae90",
"months": [
{
"month": "January",
"total": 15000,
"envelopes": [
"635cae5428135652527ce99d",
"635d1cab58ec2d6f8995f82e"
],
"_id": "635997a3a7099a37783cae84",
"remaining": -2000,
"spent": 2000
}
]
}
however I can’t seem to just get the envelope array from it. I tried originally using projection but that returned me the envelopes from every month so I am currently stuck on what to do