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
相关推荐
橡晟3 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)墨尘游子3 小时前
神经网络的层与块倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊企鹅与蟒蛇4 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决autobaba4 小时前
编写bat文件自动打开chrome浏览器,并通过selenium抓取浏览器操作chromeRvelamen5 小时前
LLM-SECURITY-PROMPTS大模型提示词攻击测评基准【本人】5 小时前
Django基础(一)———创建与启动SHIPKING3936 小时前
【python】基于pygame实现动态粒子爱心kk_stoper7 小时前
如何通过API查询实时能源期货价格java1234_小锋7 小时前
【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 架构搭建