Skip to content
Advertisement

pymongo converts . variables into a dict

I am inserting the data to mongoDB collection through pymongo. I have logged all the information and data which is being sent to update_one statement.

Data which is logged just before update_one statement :

 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'}

But when it got inserted into “test” then it got appended like this :

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"} 

Using this to update the document:

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

So here you’ll see parsed data with same prefix keys ThingName. get converted into a dict in mongo collection with key as ThingName. WHy this is happening and how can we override this?

Advertisement

Answer

That’s perfectly valid. Because when you update with thing.a:x, then it will store as object thing: { a : x}

Field names restrictions

Though mongo supports dot in latest versions, drivers do not support them yet. Hence the conversion still happens.

Another wonderful post

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement