Here is my code
const matchObjSensor = {
$match: {
time: {
$gte: eventStat.time.from,
$lte: eventStat.time.to,
},
dat: { $regex: "^" + eventStat.dat[1] },
},
};
const projectObjSensor = {
$project: {
h: { $hour: { date: { $toDate: "$time" }, timezone: localTimeZone } },
"meter.RadiationTemperature": 1,
"meter.PVPannelTemperature": 1,
meterId:1
},
};
const groupObjSensor = {
$group: {
_id: {
hour: "$h",
meter: "$meterId",
},
lastRadiationTemp: {
$last: "$meter.RadiationTemperature",
},
lastModuleTemp: {
$last: "$meter.PVPannelTemperature",
},
},
};
const eventsResult1 = await EventModel.aggregate([
matchObjSensor,
projectObjSensor,
groupObjSensor,
]);
Now match object returns a set of documents that can be grouped or separated on the basis of meterId, I want to find RadiationTemp and PanelTemperature which can be found in only 1 type of meterId suppose “x”, the problem is when I use $last it often returns null at that hour because it takes into account a document where meterId is different not “x” and RadiationTemp field does not exist, now for an hour I only want to consider documents with meterId-x so $last does not return null which it does now because for an hour it also takes into account other documents where meterId is not x but the two fields are missing