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
相关推荐
零度@15 分钟前
Java中Map的多种用法550A23 分钟前
如何修改kagglehub的数据集默认下载路径倚天仗剑走天涯WGM26 分钟前
对CANoe和VBA和TSmaster 三款工具的调用的理解我命由我1234527 分钟前
python-dotenv - python-dotenv 快速上手黑客思维者27 分钟前
Python定时任务schedule/APScheduler/Crontab 原理与落地实践yaoxin52112333 分钟前
268. Java Stream API 入门指南测试人社区-千羽38 分钟前
48小时攻克测试岗——闪电面试极速备战手册weixin_3903084643 分钟前
Python工具网站网址电饭叔1 小时前
指定私有浮点数据域get/set方法等《python语言程序设计》2018版--第8章18题第1部分kiki-bf1 小时前
使用python把图片转为word