python - 提取mongodb文档并在python中创建json文件
问题描述
我有 mongodb 文档作为数组,需要在 python 中创建 json 文件。
mongodb文档看起来像
db.mappedfields.insertMany(
[
{
sourceAttribute: "first_name",
domainAttribute: "First_Name"
},
{
sourceAttribute: "last_name",
domainAttribute: "Last_Name"
}
]
) 代码尝试
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient["guid"]
mycol = mydb["mappedfields"]
cursor = mycol.find({},{'_id':False})
list_cur = list(cursor)
json_data = dumps(list_cur, indent=1)
with open('mapping_files/mapping_from_mongodb.json', 'w') as file:
file.write(json_data)
获取输出
[
{
"sourceAttribute": "first_name",
"domainAttribute": "First_Name"
},
{
"sourceAttribute": "last_name",
"domainAttribute": "Last_Name"
}
]
预期输出:
{
"first_name": "First_Name",
"last_name": "Last_Name"
}
解决方案
推荐阅读
- r - 使用新 R 版本更新旧 R 版本中的包
- neo4j - Neo4j 和 Cypher:只反转一个箭头
- exception - 关于哪个 OCaml 标准库函数可以引发哪个错误的文档
- python - FreeOPC 不会在启动时启动
- ios - swift - 使用 Decodable 从 API 解析 JSON
- react-native - 在 react native 中停止启动类似于 TikTok 的视频录制栏
- java - 如何为 Junit 测试模拟 executeNamedQuery(query, params)
- aws-lambda - Pdf to Png with Python in Lambda- 错误:“无法获取页数。是否已安装 poppler 并在 PATH 中?”
- pine-script - 无法从 pine 编辑器中的函数访问值
- rx-java - 在 Vert.x 中重试 HTTP 调用 N 次,然后返回一个 Maybe