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
相关推荐
.54817 分钟前
## Sorting(排序算法)ydmy20 分钟前
注意力机制(个人理解)iwhitney1 小时前
【次方量化】3分钟搞懂什么是量化策略高洁012 小时前
大模型部署资源不足?轻量化部署解决方案阿里云大数据AI技术2 小时前
MaxFrame 视频帧智能分析:从视频到语义向量的端到端分布式处理淘矿人2 小时前
从0到1:用Claude启动你的第一个项目嘻嘻哈哈樱桃2 小时前
牛客经典101题题解集--动态规划gmaajt2 小时前
Golang怎么做国际化多语言_Golang i18n教程【核心】maqr_1103 小时前
CSS如何利用Sass定义全局阴影方案_通过变量实现统一CSS风格m0_613856293 小时前
uni-app怎么做类似于美团的商家评价星级 uni-app五星评分组件制作【实战】