我使用python 3.9.12查询mongodb,然后我将值读入变量并继续执行我的逻辑。问题是,我的一些值的键以美元符号开头。下面是我得到的一个json示例:
[
{
"_id": {
"$oid": "234876234875236752309823"
},
"createdAt": {
"$date": "2022-11-13T20:50:18.184Z"
},
"moreFields": {
"key1": "blabla1",
"key2": "blabla2",
"key3": "blabla3"
},
"entityId": {
"$binary": {
"base64": "z0kWDTHiSlawpI2wHjyrWA==",
"subType": "04"
}
}
}
]
我理解这些mongodb字段类型(bson, datetime…)。但是这让我在尝试使用python访问这些值时变得很困难。
我正在阅读和寻找,但我找不到一种方法将它们转换为"正常"钥匙。理想情况下,我想纠正我的mongodb查询(获取datetime作为字符串和$binary作为UUID字符串)。我在python中发现了一个愚蠢的解决方案,但不幸的是它非常愚蠢,我想纠正我的方式。
任何想法?谢谢:)
我真的很高兴,如果我的mongodb查询的结果会改变为:
[
{
"_id": "234876234875236752309823",
"createdAt": "2022-11-13T20:50:18.184Z",
"moreFields": {
"key1": "blabla1",
"key2": "blabla2",
"key3": "blabla3"
},
"entityId": "e87b22b2-ea15-4176-9100-c65f79f0e5b2"
}
]
如果您的数据是字符串格式(例如,来自文件),则使用bson.json_util
模块中的loads
。https://pymongo.readthedocs.io/en/stable/api/bson/json_util.html
对于第二部分,这只是格式化;但是要注意,这只会创建另一个字符串输出。有可能您感兴趣的数据实际上就在记录对象中。
下面的代码片段转换输入字符串,将其加载到MongoDB中,然后使用自定义编码器将其格式化为字符串:
import datetime
import json
import bson
from bson import json_util
from pymongo import MongoClient
db = MongoClient()['mydatabase']
records = '''[
{
"_id": {
"$oid": "234876234875236752309823"
},
"createdAt": {
"$date": "2022-11-13T20:50:18.184Z"
},
"moreFields": {
"key1": "blabla1",
"key2": "blabla2",
"key3": "blabla3"
},
"entityId": {
"$binary": {
"base64": "z0kWDTHiSlawpI2wHjyrWA==",
"subType": "04"
}
}
}
]'''
db.mycollection.insert_many(json_util.loads(records))
class MyJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat() # Format dates as ISO strings
elif isinstance(obj, bson.Binary) and obj.subtype == bson.binary.UUID_SUBTYPE:
return obj.as_uuid() # Format binary data as UUIDs
elif hasattr(obj, '__str__'):
return str(obj) # This will handle ObjectIds
return super(MyJsonEncoder, self).default(obj)
record = db.mycollection.find_one()
print(json.dumps(record, cls=MyJsonEncoder, indent=4))
打印:
{
"_id": "234876234875236752309823",
"createdAt": "2022-11-13T20:50:18.184000",
"moreFields": {
"key1": "blabla1",
"key2": "blabla2",
"key3": "blabla3"
},
"entityId": "cf49160d-31e2-4a56-b0a4-8db01e3cab58"
}