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 小时前
python学习篇WoY20203 小时前
opencv-python在ubuntu系统中缺少依赖大游小游之老游5 小时前
Python中如何实现一个程序运行时,调用另一文件中的函数mantch5 小时前
个人 LLM 接口服务项目:一个简洁的 AI 入口weixin_445054725 小时前
力扣热题51朱朱没烦恼yeye5 小时前
java基础学习databook6 小时前
数据可视化五大黄金原则:让你的图表“会说话”ai_top_trends6 小时前
2026 年度工作计划 PPT 模板与 AI 生成方法详解智航GIS6 小时前
9.4 Word 自动化2501_941809146 小时前
面向多活架构与数据地域隔离的互联网系统设计思考与多语言工程实现实践分享记录