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)
相关推荐
大江东去浪淘尽千古风流人物21 分钟前
【Sensor】IMU传感器选型车轨级 VS 消费级
人工智能·python·算法·机器学习·机器人
码农小韩2 小时前
AIAgent应用开发——DeepSeek分析(一)
人工智能·python·深度学习·agent·强化学习
学Linux的语莫2 小时前
skills的使用
java·数据库·python
像风一样的男人@2 小时前
python --打包pyd或so文件
开发语言·python
MediaTea3 小时前
Python:生成器对象的扩展接口
开发语言·网络·python
Katecat996634 小时前
基于FCOS-HRNetV2P的高空作业安全装备检测与违规行为识别系统
python
好家伙VCC4 小时前
# 发散创新:基于Python的轻量级测试框架设计与实践 在现代软件开发中,**自动化
java·开发语言·python·自动化
007张三丰4 小时前
软件测试专栏(5/20):自动化测试入门指南:从零开始构建你的第一个测试框架
自动化测试·python·算法·压力测试·测试框架
NGC_66114 小时前
Java异常体系
开发语言·python
tang777894 小时前
深挖66免费代理网站:隐藏功能与真实体验报告
爬虫·python·网络爬虫·ip