【问题标题】:Upsert with pymongo and a custom _id field使用 pymongo 和自定义 _id 字段进行更新
【发布时间】:2013-03-07 03:20:38
【问题描述】:

我正在尝试根据this document 将预先聚合的性能指标存储在分片 mongodb 中。

我正在尝试使用这样的 upsert 更新可能存在或不存在的记录中的分钟子文档(self.collection 是一个 pymongo 集合实例):

self.collection.update(query, data, upsert=True)

查询:

{   '_id': u'12345CHA-2RU020130304',
    'metadata': {   'adaptor_id': 'CHA-2RU',
                    'array_serial': 12345,
                    'date': datetime.datetime(2013, 3, 4, 0, 0, tzinfo=<UTC>),
                    'processor_id': 0}
}

数据:

{   'minute': {   '16': {   '45': 1.6693091}}}

问题在于,在这种情况下,“分钟”子文档始终只有最后一个 hour: { minute: metric} 条目,分钟子文档不会为其他时间创建新条目,它总是覆盖一个条目。

我也尝试过使用 $set 样式的数据条目:

{ '$set': {   'minute': {   '16': {   '45': 1.6693091}}}}

但它最终是一样的。

我做错了什么?

【问题讨论】:

    标签: mongodb pymongo


    【解决方案1】:

    在列出的两个示例中,您只是将字段 ('minute') 设置为特定值,它是第一次更新时添加的唯一原因是因为该字段本身不存在,因此必须创建.

    很难确切地确定您要在这里拍摄什么,但我认为您可以做的是稍微改变您的架构,以便 'minute' 是一个数组。然后,您可以使用 $push 添加值,无论它们是否已经存在,或者如果您不想重复,则使用 $addToSet

    我不得不稍微修改一下您的文档以使其在 shell 中有效,所以我的 _id(和其他一些字段)与您的略有不同,但它应该仍然足够接近以便说明:

    db.foo.find({'_id': 'u12345CHA-2RU020130304'}).pretty()
    {
            "_id" : "u12345CHA-2RU020130304",
            "metadata" : {
                    "adaptor_id" : "CHA-2RU",
                    "array_serial" : 12345,
                    "date" : ISODate("2013-03-18T23:28:50.660Z"),
                    "processor_id" : 0
            }
    }
    

    现在让我们添加一个包含文档数组而不是单个文档的 minute 字段:

    db.foo.update({'_id': 'u12345CHA-2RU020130304'}, { $addToSet : {'minute': { '16': {'45': 1.6693091}}}})
    db.foo.find({'_id': 'u12345CHA-2RU020130304'}).pretty()
    {
            "_id" : "u12345CHA-2RU020130304",
            "metadata" : {
                    "adaptor_id" : "CHA-2RU",
                    "array_serial" : 12345,
                    "date" : ISODate("2013-03-18T23:28:50.660Z"),
                    "processor_id" : 0
            },
            "minute" : [
                    {
                            "16" : {
                                    "45" : 1.6693091
                            }
                    }
            ]
    }
    

    然后,为了说明添加,添加一个稍微不同的条目(因为我使用的是$addToSet,这是添加新字段所必需的:

    db.foo.update({'_id': 'u12345CHA-2RU020130304'}, { $addToSet : {'minute': { '17': {'48': 1.6693391}}}})
    db.foo.find({'_id': 'u12345CHA-2RU020130304'}).pretty()
    {
            "_id" : "u12345CHA-2RU020130304",
            "metadata" : {
                    "adaptor_id" : "CHA-2RU",
                    "array_serial" : 12345,
                    "date" : ISODate("2013-03-18T23:28:50.660Z"),
                    "processor_id" : 0
            },
            "minute" : [
                    {
                            "16" : {
                                    "45" : 1.6693091
                            }
                    },
                    {
                            "17" : {
                                    "48" : 1.6693391
                            }
                    }
            ]
    }
    

    【讨论】:

    • 不会将hours: minutes 存储为数组中的项目来否定我上面提到的链接中概述的寻求好处吗?此外,您在“_id”中包含的“u”并不意味着存在,它是一个表示 unicode 的 python 打印工件。谢谢,我会考虑的。
    【解决方案2】:

    我最终设置了这样的字段:

    查询:

    {   '_id': u'12345CHA-2RU020130304',
        'metadata': {   'adaptor_id': 'CHA-2RU',
                        'array_serial': 12345,
                        'date': datetime.datetime(2013, 3, 4, 0, 0, tzinfo=<UTC>),
                        'processor_id': 0}
    }
    

    我正在设置这样的指标:

    data = {"$set": {}}
    
    for metric in csv:
      date_utc = metric['date'].astimezone(pytz.utc)
      data["$set"]["minute.%d.%d" % (date_utc.hour,
                                    date_utc.minute)] = float(metric['metric'])
    

    创建这样的数据:

    {"$set": {'minute.16.45': 1.6693091,
              'minute.16.46': 1.566343,
              'minute.16.47': 1.22322}}
    

    这样当self.collection.update(query, data, upsert=True) 运行时,它会更新这些字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 2020-11-08
      • 2018-07-22
      相关资源
      最近更新 更多