【问题标题】:Bucket pattern for time-series data mongodb with python pymongo时间序列数据 mongodb 的存储桶模式与 python pymongo
【发布时间】:2021-04-02 16:51:26
【问题描述】:

我想创建基于时间的存储桶,如果需要,特别是每小时或更长时间。我在https://docs.mongodb.com/manual/tutorial/model-time-data/#example 阅读了有关存储桶模式的信息,但我不知道与 python pymongo 一起使用什么代码。我的数据集包含 2010 年的 11 个文件-2020 及其大约 150 万行,如下所示:

_id:ObjectId("603fb0b7142a0cbb439ae2e1")
    id1:3758
    id6:2
    id7:-79.09
    id8:35.97
    id9:5.5
    id10:0
    id11:-99999
    id12:0
    id13:-9999
    c14:"U"
    id15:0
    id16:99
    id17:0
    id18:-99
    id19:-9999
    id20:33
    id21:0
    id22:-99
    id23:0
    timestamp1:2010-01-01T00:05:00.000+00:00
    timestamp2:2009-12-31T19:05:00.000+00:00

所有属性每 5 分钟更改一次,但 id1 保持不变。这是我尝试过的(在处理文件并将它们转换为 df 之后):

files =  os.listdir('sampl/')
sorted_files =  sorted(files)

for file in sorted_files:
    df = process_file(file)
    #df.reset_index(inplace=True)  # Reset Index
    data_dict = df.to_dict('records')  # Convert to dictionary

    mycol1.update_many(
        {'nsamples': {'$lt': 12}},
        {
            '$push': {'samples': data_dict },
            '$min': {'first': df['timestamp1']},
            '$max': {'last': df['timestamp1']},
            '$inc': {'nsamples': 1}
        },
        upsert=True
    )

输出: bson.errors.InvalidDocument: cannot encode object: id1 id6 id7 ... id23 timestamp1 timestamp2 任何帮助将不胜感激!提前致谢!

【问题讨论】:

    标签: python mongodb dataframe time-series pymongo


    【解决方案1】:

    这里是关于如何在 mongodb 中插入带有桶模式的数据的答案:

    for file in sorted_files:
        df = process_file(file)
        for row,item in df.iterrows():
            data_dict = item.to_dict()
            id1=3758
            mycol1.update_many(
                {"id1":id1,"nsamples": {"$lt": 12}},
                {
                    "$push": {"id24": data_dict},
                    "$min": {"first": data_dict['timestamp1']},
                    "$max": {"last": data_dict['timestamp1']},
                    "$inc": {"nsamples": 1}
                },
                upsert=True
            )
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2012-11-16
      • 2014-01-03
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      相关资源
      最近更新 更多