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)
相关推荐
zl_dfq4 分钟前
Python学习5 之【字符串】
python·学习
ZC跨境爬虫14 分钟前
Python异步IO详解:原理、应用场景与实战指南(高并发爬虫首选)
爬虫·python·算法·自动化
倦王26 分钟前
力扣日刷47-补
python·算法·leetcode
2501_921649491 小时前
原油期货量化策略开发:历史 K 线获取、RSI、MACD 布林带计算到多指标共振策略回测
后端·python·金融·数据分析·restful
真心喜欢你吖1 小时前
统信操作系统UOS部署安装OpenClaw+飞书接入完整教程(国产大模型配置)
人工智能·python·语言模型·大模型·openclaw·小龙虾
用户8356290780511 小时前
使用 Python 自动生成 Excel 柱状图的完整指南
后端·python
xcbrand1 小时前
口碑好的品牌策划厂家
大数据·人工智能·python
liu****1 小时前
LangChain-AI应用开发框架(七)
人工智能·python·langchain·大模型应用·本地部署大模型
hhcgchpspk1 小时前
网速上传下载流量监测工具尝试
网络·python·cmd·psutil
飞Link2 小时前
大模型时代的“语言编程”:Prompt Engineering (提示词工程) 深度解析与实战指南
开发语言·python·prompt