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
相关推荐
happylifetree8 小时前
Python70:核心语法-函数进阶-案例2王中阳Go8 小时前
两个入口答案不一致先别调模型:6 行入口口径对账表(附脚本)BK贝壳9 小时前
bool-hybrid-array 落盘我只认 .bha:用 Create_BHA / Ask_BHA 做布尔数组序列化Sakura_j_h_9 小时前
Python 在定时任务里输出 emoji,UnicodeEncodeError 从哪来supabc12310 小时前
欧盟电子发票(XRechnung/Peppol/ZUGFeRD)踩坑记:用 Python 不装 Java 跑官方校验规则ZhengEnCi11 小时前
AI Agent工具调用防护层怎么写-一份能直接跑的Python四层拦截实现ZhengEnCi11 小时前
AI Agent工具误调用怎么解决-从参数校验到高危二次确认的四层工程防线镜子AI12 小时前
镜子AI工程教程:用Python实现关键词布局检查器与语义改写查重(附完整代码)小oo呆13 小时前
【学习心得】Python协程信誓旦旦的程序猿14 小时前
【Python量化实战宝典 #22】涨跌停、连板情绪手册:情绪指标构建(Python 可跑)