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)
相关推荐
零度@15 分钟前
Java中Map的多种用法
java·前端·python
550A23 分钟前
如何修改kagglehub的数据集默认下载路径
python
倚天仗剑走天涯WGM26 分钟前
对CANoe和VBA和TSmaster 三款工具的调用的理解
python
我命由我1234527 分钟前
python-dotenv - python-dotenv 快速上手
服务器·开发语言·数据库·后端·python·学习·学习方法
黑客思维者27 分钟前
Python定时任务schedule/APScheduler/Crontab 原理与落地实践
开发语言·python·crontab·apscheduler
yaoxin52112333 分钟前
268. Java Stream API 入门指南
java·开发语言·python
测试人社区-千羽38 分钟前
48小时攻克测试岗——闪电面试极速备战手册
人工智能·python·opencv·面试·职场和发展·单元测试·压力测试
weixin_3903084643 分钟前
Python工具网站网址
python
电饭叔1 小时前
指定私有浮点数据域get/set方法等《python语言程序设计》2018版--第8章18题第1部分
开发语言·python
kiki-bf1 小时前
使用python把图片转为word
开发语言·python·word