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
相关推荐
kszlgy43 分钟前
Day 52 神经网络调参指南wrj的博客2 小时前
python环境安装Pyeako2 小时前
深度学习--BP神经网络&梯度下降&损失函数摘星编程3 小时前
OpenHarmony环境下React Native:Geolocation地理围栏充值修改昵称4 小时前
数据结构基础:从二叉树到多叉树数据结构进阶q_35488851536 小时前
AI大模型:python新能源汽车推荐系统 协同过滤推荐算法 Echarts可视化 Django框架 大数据毕业设计(源码+文档)✅Yeats_Liao6 小时前
开源生态资源:昇腾社区ModelZoo与DeepSeek的最佳实践路径被星1砸昏头6 小时前
掌握Python魔法方法(Magic Methods)love530love7 小时前
彻底解决 ComfyUI Mixlab 插件 Whisper.available False 的报错不解风水7 小时前
《深度学习入门:基于 Python 的理论与实现》(斋藤康毅)