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
相关推荐
Zonda要好好学习15 分钟前
Python入门Day4小龙在山东1 小时前
Python 包管理工具 uvweixin_307779131 小时前
批量OCR的GitHub项目孤狼warrior2 小时前
灰色预测模型神仙别闹2 小时前
基于Python实现LSTM对股票走势的预测机器学习之心3 小时前
小波增强型KAN网络 + SHAP可解释性分析(Pytorch实现)JavaEdge在掘金3 小时前
MySQL 8.0 的隐藏索引:索引管理的利器,还是性能陷阱?站大爷IP3 小时前
Python办公自动化实战:手把手教你打造智能邮件发送工具chao_7893 小时前
回溯题解——子集【LeetCode】二进制枚举法zdw4 小时前
fit parse解析佳明.fit 运动数据,模仿zwift数据展示