【问题标题】:pymongo converts . variables into a dictpymongo 转换。变量到字典中
【发布时间】:2021-04-23 13:28:33
【问题描述】:

我正在通过 pymongo 将数据插入到 mongoDB 集合中。我已经记录了发送到 update_one 语句的所有信息和数据。

在 update_one 语句之前记录的数据:

 data =   {'a': 'h9421976fc124d5756497d3b', 'b': 1611046532.4558306, 'kw_trigger_thing_name': 'ThingName.a', 'ThingName.a_capability_temperature': 44, 'ThingName.a_capability_humidity': '288', 'ThingName.a_kw_thing_name': 'ThingName.a'}

但是当它被插入“测试”时,它会像这样被附加:

inserted_data = { "_id" : ObjectId("6005d317525e0d67866c564f"), "a" : "h9421976fc124d5756497d3b", "b" : 1611046532.4558306, "ThingName" : { "a_capability_humidity" : "288", "a_capability_temperature" : 44,"a_kw_thing_name" : "ThingName.a"} 

使用它来更新文档:

collection.update_one({"a": "h9421976fc124d5756497d3b"},{"$set": data},upsert=True,)

因此,您将在此处看到具有相同前缀键 ThingName 的已解析数据。在 mongo 集合中转换为 dict,键为 ThingName。 为什么会发生这种情况,我们如何才能覆盖它?

【问题讨论】:

    标签: python python-3.x mongodb pymongo pymongo-3.x


    【解决方案1】:

    这是完全正确的。因为当您使用thing.a:x 更新时,它将存储为对象thing: { a : x}

    Field names restrictions

    虽然 mongo 在最新版本中支持 dot,但 drivers 还不支持它们。因此转换仍然发生。

    Another wonderful post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多