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
相关推荐
将心ONE11 分钟前
pip导出项目依赖shx666621 分钟前
2.2.1 ROS2 在功能包中编写 Python 节点beijingliushao23 分钟前
100-Spark Local模式部署BoBoZz1926 分钟前
TessellatedBoxSource 创建并渲染一个细分的、可移动的箱体模型weixin_4573402137 分钟前
旋转OBB数据集标注查看器nvd111 小时前
LLM 对话记忆功能实现深度解析电饭叔1 小时前
Luhn算法初介绍badmonster01 小时前
实时代码库索引:用 CocoIndex 构建智能代码搜索的终极方案晓山清1 小时前
Meeting Summarizer Using Natural Language Processing论文理解zqy02271 小时前
python安装与环境配置