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
相关推荐
时空系12 小时前
第12篇:文档操作——文件读写 python中文编程2501_9010064712 小时前
Golang map底层实现原理_Golang map哈希表原理教程【收藏】keineahnung234512 小时前
為什麼這個 Tensor 算 dense?PyTorch _eval_is_non_overlapping_and_dense 深入解析寒山独见君~12 小时前
自动化-消息推送Server酱3,APP推送qq_3926906612 小时前
Go语言怎么做DNS查询_Go语言DNS域名解析教程【完整】m0_7381207212 小时前
后渗透维权提权基础——CTF模拟红队进行权限维持(二)speop12 小时前
Reasoning kingdom chapter13m0_6315298212 小时前
PHP 中 OR 运算符逻辑误用的典型陷阱与正确写法步辞12 小时前
如何用 style.setProperty 修改带有优先级的 CSS 变量属性AC赳赳老秦12 小时前
团队知识库搭建:用 OpenClaw 自动整理会议纪要、技术方案、故障复盘,同步到 Confluence / 语雀