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
相关推荐
JamSlade1 小时前
SSO登录验证设计要点细节(以微软 Microsoft SSO为例) 基于react pythonMediaTea2 小时前
Python 文件操作:JSON 格式百锦再2 小时前
金仓数据库提出“三低一平”的迁移理念野生工程师2 小时前
【Python爬虫基础-1】爬虫开发基础力江2 小时前
攻克维吾尔语识别的技术实践(多语言智能识别系统)诗句藏于尽头2 小时前
MediaPipe+OpenCV的python实现交互式贪吃蛇小游戏盼哥PyAI实验室3 小时前
Python 正则表达式实战 + 详解:从匹配QQ邮箱到掌握核心语法木易 士心3 小时前
Android 开发核心技术深度解析nju_spy3 小时前
力扣每日一题(四)线段树 + 树状数组 + 差分lzq6033 小时前
Python虚拟环境全指南:venv与conda对比与实践