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
相关推荐
:mnong4 分钟前
附图报价系统设计分析3AmyLin_20015 分钟前
【pdf2md-2:关键核心】PDF 转 Markdown 技术拆解:两阶段流水线、四级标题检测与段落智能合并薛不痒8 分钟前
Llamafactory的使用(1)不喝水的鱼儿10 分钟前
KT Qwen3.5-35B-A3B 记录小陈工18 分钟前
Python Web开发入门(三):配置文件管理与环境变量最佳实践deep_drink23 分钟前
1.1、Python 与编程基础:开发环境、基础工具与第一个 Python 项目杨超越luckly37 分钟前
HTML应用指南:利用GET请求获取中国生活垃圾焚烧发电厂位置信息Genios39 分钟前
今天是我正式开启Python学习之旅的第7天maxmaxma41 分钟前
ROS2机器人少年创客营:Python第一课源码之家1 小时前
计算机毕业设计:汽车销售数据采集分析系统 Flask框架 requests爬虫 可视化 数据分析 大数据 机器学习 大模型(建议收藏)✅