python实现数据库两个表之间的更新操作(模糊匹配)示例

复制代码
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)
相关推荐
武子康1 天前
Java-167 Neo4j CQL 实战:CREATE/MATCH 与关系建模速通 案例实测
java·开发语言·数据库·python·sql·nosql·neo4j
upward_tomato1 天前
python中模拟浏览器操作之playwright使用说明以及打包浏览器驱动问题
开发语言·python
为你写首诗ge1 天前
【python】python安装使用pytorch库环境配置
pytorch·python
信创天地1 天前
RISC-V 2025年在国内的发展趋势
python·网络安全·系统架构·系统安全·运维开发
Danceful_YJ1 天前
30.注意力汇聚:Nadaraya-Watson 核回归
pytorch·python·深度学习
FreeCode1 天前
LangChain1.0智能体开发:人机协作
python·langchain·agent
2501_930412271 天前
如何添加清华源到Conda?
开发语言·python·conda
2501_930412271 天前
如何删除Conda中的清华源配置?
开发语言·python·conda
程序猿20231 天前
Python每日一练---第十二天:验证回文串
开发语言·python
沿着路走到底1 天前
python 判断与循环
java·前端·python