from pymongo import MongoClient
import pandas as pd
# 连接到MongoDB(这里假设MongoDB运行在本地默认端口上)
mgclient = MongoClient('localhost', 27017)
# 选择数据库(如果你没有指定数据库,MongoDB会使用默认的'test'数据库)
db = mgclient['test'] # 替换'your_database_name'为你的数据库名
test1_collection=db["test1"].find({})
test1_list=list(test1_collection)
test2_collection=db["test2"].find({})
test2_list=list(test2_collection)
# 这种方式只适合插入存在的数据。
for document1 in test1_list:
document1.pop("_id")
document1["codename"]=document1["code"]
# document1["codename"] = document1["code"]
for document2 in test2_list:
# dataframe包含关系不是很好merge
if document1["code"]==document2["code"] and document1["code"].startswith(document2["code"]):
document1["codename"]=document2["name"]
print(document1)
# 如果只用写入能够匹配上的,在这里insert
# db["test3"].insert_one(document1)
db["test3"].insert_one(document1)
python实现数据库两个表之间的更新操作(模糊匹配)示例
半吊子的程序狗2024-10-28 15:27
相关推荐
QQ2422199791 天前
基于python+微信小程序的家教管理系统_mh3j9RSTJ_16251 天前
PYTHON+AI LLM DAY THREETY-SEVEN郝学胜-神的一滴1 天前
深度学习优化核心:梯度下降与网络训练全解析Aision_1 天前
Agent 为什么需要 Checkpoint?清水白石0081 天前
《Python性能深潜:从对象分配开销到“小对象风暴”的破解之道(含实战与最佳实践)》Land03291 天前
RPA工具选型技术指南:架构差异与实测数据kafei_*1 天前
VScode 添加 UV虚拟环境方法洛_尘1 天前
Python 5:使用库m0_596749091 天前
如何防止SQL拼接漏洞_使用PDO对象实现安全的SQL交互AIFQuant1 天前
2026 全球股票/外汇/贵金属行情 API 深度对比:延迟、覆盖、价格与稳定性