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
相关推荐
hhzz3 小时前
基于监控视频的水位尺自动识别技术方案与实现yongche_shi3 小时前
ragas官方文档中文版(五十)weixin_408099674 小时前
OCR批量识别图片方案:从手动处理到自动化API系统(Python/Java/PHP实战)AI行业学习4 小时前
Notepad++ 官方下载 + 完整安装 + 全套优化配置(2026最新)大圣编程5 小时前
Python中continue语句的用法是什么?云烟成雨TD5 小时前
LangFlow 1.x 系列【5】可视化编辑页面功能说明geovindu7 小时前
python: Functional Options PatterntryCbest7 小时前
Python 文件操作涛声依旧-底层原理研究所7 小时前
Agent 长任务可靠性设计:实现暂停、恢复、续跑与崩溃重启的完整方案AC赳赳老秦8 小时前
防火墙规则批量配置实战:OpenClaw 自动生成模板、批量下发与合规性校验全解析