mongodb 聚合:在输出中连接 _id.day、_id、月份、_id.year

mongodb aggregation: concatenate _id.day, _id,month, _id.year in output

我有以下 mongodb 聚合:

{
    "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。我该怎么做?

您想在使用 $toString and $concat

返回结果之前更改 _id 的结构
{
    "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"},
                    ]
                }
            }
        }
    ]
}