0

刚刚开始学习mongodb,想要尝试分桶,目前显示的问题是要使用$getField~~求指点修改

目的:

Mogodb中test1数据库中已经存在集合location,存储的文档格式为: { “_id” : ObjectId(“64548df05f6016c6e607442f”), “gid” : “G001301003_12300”, “height” : 300.0, “lon” : 105.0, “lat” : 33.0, “temperature” : 26.41, “humidity” : 18.75, “timestamp” : “2021-07-05 10:01:00” }

如何对该集合数据进行时间按天分桶,形成新的集合location_bucket,使得新集合的文档存储格式为:

{ “_id” : ObjectId(“64548df05f6016c6e607442f”), “gid” : “G001301003_12300”, “height” : 300.0, “lon” : 105.0, “lat” : 33.0,

“weather”:{ //1点 “0“:{ “temperature” : 26.41, “humidity” : 18.75, }, “1“:{ “temperature” : 26.41, “humidity” : 18.75, }, “23“:{ “temperature” : 26.41, “humidity” : 18.75, }, } }

目前代码为:

from pymongo import MongoClient
from datetime import datetime, timedelta
# 连接Mongodb数据库
client = MongoClient('localhost', 27017)
db = client['test1']

pipeline = [
    {
        "$group": {
            "_id": {
                "gid": "$gid",
                "date": {
                    "$dateToString": {
                        "format": "%Y-%m-%d",
                        "date": {
                            "$toDate": "$timestamp"
                        }
                    }
                }
            },
            "data": {
                "$push": {
                    "hour": {
                        "$hour": {
                            "$toDate": "$timestamp"
                        }
                    },
                    "temperature": "$temperature",
                    "humidity": "$humidity"
                }
            }
        }
    },
    {
        "$project": {
            "_id": 0,
            "gid":"$_id.gid",
            "date": "$_id.date",
            "weather": {
                "$arrayToObject": {
                    "$map": {
                        "input": "$data",
                        "as": "el",
                        "in": {
                            "$let": {
                                "vars": {
                                    "hour": {
                                        "$toString": "$$el.hour"
                                    }
                                },
                                "in": {
                                    "$arrayToObject": {
                                        "$map": {
                                            "input": [
                                                "humidity",
                                                "temperature"
                                            ],
                                            "as": "field",
                                            "in": {
                                                "k": "$$field",
                                                "v": "$$el.$$field"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    {
        "$out": "location_bucket"
    }
]
# 执行聚合操作
db.location.aggregate(pipeline)

#执行错误
更改状态以发布