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)
相关推荐
lbb 小魔仙几秒前
Python_多模态大模型实战指南
开发语言·python
XDHCOM16 分钟前
Python os.system() 和 subprocess 怎么选?运行系统命令哪个更好用?
开发语言·网络·python
雨墨✘26 分钟前
golang如何实现设备指纹识别_golang设备指纹识别实现详解
jvm·数据库·python
yfndsb35 分钟前
从入门到落地:OpenClaw 全面介绍与全平台本地部署保姆级教程
人工智能·python·ai
qq_2837200535 分钟前
Python Web 开发:Flask 快速入门教程
python·flask·web
加勒比海带661 小时前
目标检测算法——低空智能实验室开放数据集汇总附下载链接【点赞+收藏】
大数据·图像处理·人工智能·python·深度学习·目标检测·计算机视觉
PILIPALAPENG1 小时前
第2周 Day 4:英语 Agent Web 版上线:从命令行到浏览器
前端·人工智能·python
用户8356290780512 小时前
Python 操作 Word 文档节与页面设置
后端·python
西西弗Sisyphus2 小时前
Python 闭包的经典坑
python·闭包
西西弗Sisyphus2 小时前
Python 在dataclasses 里,field() 能给可变、不可变数据分别设置安全的默认值
python·field·dataclasses