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
相关推荐
多米Domi0118 小时前
0x3f第33天复习 (16;45-18:00)freepopo8 小时前
天津商业空间设计:材质肌理里的温度与质感[特殊字符]森叶8 小时前
Java 比 Python 高性能的原因:重点在高并发方面小二·9 小时前
Python Web 开发进阶实战:混沌工程初探 —— 主动注入故障,构建高韧性系统Lkygo9 小时前
LlamaIndex使用指南小二·9 小时前
Python Web 开发进阶实战:低代码平台集成 —— 可视化表单构建器 + 工作流引擎实战Wise玩转AI9 小时前
团队管理:AI编码工具盛行下,如何防范设计能力退化与知识浅薄化?赵谨言10 小时前
Python串口的三相交流电机控制系统研究鹿角片ljp10 小时前
Engram 论文精读:用条件记忆模块重塑稀疏大模型Blossom.11810 小时前
AI Agent的长期记忆革命:基于向量遗忘曲线的动态压缩系统