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
相关推荐
gmaajt16 分钟前
mysql如何检查数据库表是否存在损坏_使用CHECK TABLE命令修复heRs BART29 分钟前
【Flask】四、flask连接并操作数据库PyHaVolask1 小时前
Python 爬虫进阶:直接请求 JSON 接口与开发者工具使用larance1 小时前
安装dify的几个问题2301_773553621 小时前
CSS如何对用户访问过的链接进行降级颜色处理_使用-visited伪类改变颜色2301_815279521 小时前
Golang怎么理解Go的sync.Pool底层_Golang如何理解Pool的本地缓存和GC清理机制【详解】2301_764150561 小时前
MySQL迁移过程如何避免数据不一致_利用强一致性备份方案m0_716430071 小时前
Redis如何处理预热失效引起的开局雪崩m0_377618231 小时前
c++文件锁使用方法 c++如何实现多进程文件同步gmaajt1 小时前
mysql多字段搜索如何设计组合索引_mysql索引查询加速