mongodb聚合:在输出中连接_id.day、_id、month、_id.year
我有以下mongodb聚合:mongodb聚合:在输出中连接_id.day、_id、month、_id.year,mongodb,aggregation,Mongodb,Aggregation,我有以下mongodb聚合: { "collection":"weather", "aggregate":[ { "$match": { "site_id": 47, "timestamp": { "$gte&
{
"collection":"weather",
"aggregate":[
{
"$match": {
"site_id": 47,
"timestamp": {
"$gte":{"$humanTime": "{{ date }}"}
}
}
},
{
"$project": {
"date": {"day": {"$dayOfMonth": "$timestamp"}, "month": {"$month": "$timestamp"}, "year": {"$year": "$timestamp"}},
"temperature": "$temperature"
}
},
{
"$group":
{
"_id": "$date",
"average_temp": {"$avg": "$temperature"},
"max_temp": {"$max": "$temperature"},
"min_temp": {"$min": "$temperature"}
}
}
]
}
这给了我以下输出:
我不想这样的约会。我想要一列“日期”,日期为dd-mm-yyyy。如何执行此操作?在使用和返回结果之前,是否要更改
\u id
的结构
你能将输出粘贴为代码而不是图像吗?我现在有一个额外的小问题,格式如下:2020-4-7我如何将其转换为类似2020-04-07 00:00的格式你可以添加
$toDate
将\u id
表达式包装到最后一个$addFields
阶段。是的,我尝试过这样做,但出现错误“$addFields”:{“convertedDate”:{“$toDate”:{“$concat”:[{“$toString”:“$\id.day”},“/”,{“$toString”:“$\id.month”},“/”,{“$toString”:“$\id.year”}}}错误是什么,应该没有问题:
{
"collection":"weather",
"aggregate":[
{
"$match": {
"site_id": 47,
"timestamp": {
"$gte":{"$humanTime": "{{ date }}"}
}
}
},
{
"$project": {
"date": {"day": {"$dayOfMonth": "$timestamp"}, "month": {"$month": "$timestamp"}, "year": {"$year": "$timestamp"}},
"temperature": "$temperature"
}
},
{
"$group":
{
"_id": "$date",
"average_temp": {"$avg": "$temperature"},
"max_temp": {"$max": "$temperature"},
"min_temp": {"$min": "$temperature"}
}
},
{
"$addFields" :{
"_id": {
"$concat": [
{"$toString": "$_id.day"},
"/",
{"$toString": "$_id.month"},
"/",
{"$toString": "$_id.year"},
]
}
}
}
]
}