我正在尝试按日期对数据进行分组(具体而言是 WaterConsumption): 如果 creationDate = creationDate 我想对 WaterConsumption 求和。 但我有点困惑 Mongoose Aggregate 是如何工作的。 我的数据:
{
"_id": "60538930f4bb883b0fe40f2f",
"creationDate": "2021-02-03T17:12:04.022Z",
"duration": 400,
"voltage": "220",
"avgPower": 6241,
"maxPower": 6473,
"WaterConsumption": 27,
"avgTemperature": 36,
"shower": "5fb56ce7734b7e04b9c97c9b",
"user": "5f6cb0496a8c5a0deaa1a746",
"__v": 0,
"id": "60538930f4bb883b0fe40f2f"
},
{
"_id": "60538bfc49a75c3ca75c0088",
"creationDate": "2021-02-16T17:21:00.615Z",
"duration": 900,
"voltage": "220",
"avgPower": 4300,
"maxPower": 5600,
"WaterConsumption": 32,
"avgTemperature": 28,
"shower": "5fb56d04734b7e04b9c97c9c",
"user": "5f6cb0496a8c5a0deaa1a746",
"__v": 0,
"id": "60538bfc49a75c3ca75c0088"
}...
我的代码:
let dateTwo = Measure.aggregate([
{$match: {creationDate: "$creationDate"}},
{$group: {_id: "$id", total: {$sum: "$WaterConsumption"}}}
])
我可以使用 JavaScript 做到这一点,但我不知道如何使用 Mongoose 做到这一点。
答案 0 :(得分:0)
我使用代码解决了我的问题:
let dateTwo = await Measure.aggregate([
{$match: {duration: 360}},
{$group: {_id: "$user", total: {$sum: "$WaterConsumption"}}}
]);