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
相关推荐
刘立军19 分钟前
使用pyHugeGraph查询HugeGraph图数据数据智能老司机4 小时前
精通 Python 设计模式——创建型设计模式数据智能老司机5 小时前
精通 Python 设计模式——SOLID 原则c8i6 小时前
django中的FBV 和 CBVc8i6 小时前
python中的闭包和装饰器这里有鱼汤9 小时前
小白必看:QMT里的miniQMT入门教程TF男孩19 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐该用户已不存在1 天前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?站大爷IP1 天前
Java调用Python的5种实用方案:从简单到进阶的全场景解析用户8356290780511 天前
从手动编辑到代码生成:Python 助你高效创建 Word 文档