把下列语句中的"P"改成取最大值:{ "aggregate" : "collection", "pipeline" : [{ "$match" : { "meterNo" : { "$in" : [ "TC11223344_1_1"]}, "createTime" : { "$gte" : { "$date" : "2023-05-29T16:00:00Z"}, "$lt" : { "$date" : "2023-05-31T03:05:12Z"}}, "P" : { "$ne" : null}}}, { "$sort" : { "createTime" : 1}}, { "$project" : { "meterNo" : 1, "P" : { "$toDouble" : "$P"}, "createTime" : { "$dateToString" : { "date" : "$createTime", "format" : "%Y-%m-%d %H:00:00", "timezone" : "Asia/Shanghai"}}}}, { "$group" : { "_id" : { "meterNo" : "$meterNo", "createTime" : "$createTime"}, "meterNo" : { "$first" : "$meterNo"}, "createTime" : { "$first" : "$createTime"}, "P" : { "$avg" : "$P"}}}], "allowDiskUse" : true}
时间: 2023-07-05 22:20:36 浏览: 127
可以将语句中的"$avg"操作改成"$max"操作,即可将"P"改成取最大值。修改后的语句如下所示:
```
{
"aggregate" : "collection",
"pipeline" : [
{ "$match" : {
"meterNo" : { "$in" : [ "TC11223344_1_1"]},
"createTime" : { "$gte" : { "$date" : "2023-05-29T16:00:00Z"}, "$lt" : { "$date" : "2023-05-31T03:05:12Z"}},
"P" : { "$ne" : null}
}
},
{ "$sort" : { "createTime" : 1}},
{ "$project" : {
"meterNo" : 1,
"P" : { "$toDouble" : "$P"},
"createTime" : { "$dateToString" : { "date" : "$createTime", "format" : "%Y-%m-%d %H:00:00", "timezone" : "Asia/Shanghai"}}
}
},
{ "$group" : {
"_id" : { "meterNo" : "$meterNo", "createTime" : "$createTime"},
"meterNo" : { "$first" : "$meterNo"},
"createTime" : { "$first" : "$createTime"},
"P" : { "$max" : "$P"}
}
}
],
"allowDiskUse" : true
}
```
阅读全文