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)
相关推荐
Dfreedom.1 分钟前
开运算与闭运算:图像形态学中的“清道夫”与“修复匠”
图像处理·python·opencv·开运算·闭运算
2301_790300965 分钟前
用Python读取和处理NASA公开API数据
jvm·数据库·python
葱明撅腚16 分钟前
利用Python挖掘城市数据
python·算法·gis·聚类
Serendipity_Carl22 分钟前
1637加盟网数据实战(数分可视化)
爬虫·python·pycharm·数据可视化·数据清洗
流㶡25 分钟前
网络爬虫之requests.get() 之爬取网页内容
python·数据爬虫
yuankoudaodaokou35 分钟前
高校科研新利器:思看科技三维扫描仪助力精密研究
人工智能·python·科技
言無咎1 小时前
从规则引擎到任务规划:AI Agent 重构跨境财税复杂账务处理体系
大数据·人工智能·python·重构
张小凡vip1 小时前
数据挖掘(十)---python操作Spark常用命令
python·数据挖掘·spark
U盘失踪了1 小时前
Reqable 导出响应数据
python
2301_790300961 小时前
数据分析与科学计算
jvm·数据库·python