Is it possible to not include Data Type names in document retrieved in PyMongo?

When I query documents in PyMongo, I will have some documents that have user specified data types (although optional) for some attributes e.g.
‘Age’: Decimal128(‘40.8’) --> Decimal128
‘JoinDate’: datetime.datetime(2020, 9, 11, 4, 0)

In the above example, I want to remove the ‘data type’ tags from the document e.g. ‘Decimal128’,‘datetime.datetime()’. For DateTime, perhaps I can use a default return a string but for other data types, how can I omit the data types? Is it possible via setting a property in PyMongo or do I need to use a regex and update the values?

Thanks much !.

Hi @Satya_Tanduri,

I think you’d have to cast the values manually in your code. Otherwise you may be able to use a custom codec to automate this somewhat. This is because MongoDB stores data in BSON datatype format, and pymongo will ensure that you don’t lose any type information. Otherwise, if the data is stored in Decimal128, then you do manipulation on the data and save the result back to the database, you could inadvertently change the datatype of the field, resulting in data inconsistencies. The same issue would be present with the datetime type.

Best regards,

Hi , it seems codec_options are meant to transform the data types e.g. convert decimal to string etc. I understand that the documents have embedded data type prefixes before the values. Was looking at ways to remove the data type prefixes only (not transform the data type). Because even if transform a Decimal to str, the data type prefix will not be removed from the document. Thanks for your guidance.