I am currently facing an issue with creating an aggregation pipeline. Everything seems to be working fine in the code until it reaches the $match section, which returns nothing. Here is the snippet of my code:
var userId = req.params.UserId
const measure = await Measure.aggregate([
{
$project: {
user: 1, creationDate: 1, WaterConsumption: 1
}
},
{
$match: {
user: userId
}
},
{
$group: {
_id: {
$dayOfMonth: "$creationDate"
},
waterConsumption: {$sum : "$WaterConsumption"}
}
}
]);
return res.json({measure})
The data I am trying to filter through is as follows:
{
"measures": [
{
"creationDate": "2021-03-19T10:25:05.674Z",
"_id": "605870bffa87a605bf2a983a",
"avgPower": 8241,
"WaterConsumption": 22,
"avgTemperature": 45,
"shower": "5fb56ce7734b7e04b9c97c9b",
"user": "5f6cb0496a8c5a0deaa1a746"
},
{
"creationDate": ""2021-03-17T10:25:05.674Z"",
"_id": "605870ebfa87a605bf2a983c"",
"avgPower": 4300,
"WaterConsumption"': 32,
'avgTemperature': 28,
'shower':"5fb56d04734b7e04b9c97c9c",
'user'': '"5f6cb0496a8c5a0deaa1a746"''
}...
]
Despite having the correct user ID value and formatting, the $match stage does not seem to be functioning correctly. The $group section works perfectly fine. Even after attempting to modify $match to $match:{user: req.params.UserId}, the result remains an empty array.