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)
相关推荐
Dontla4 小时前
用pip install -e .开发Python包时,Python项目目录结构(项目结构)(可编辑安装editable install)
python·pip
Thomas.Sir4 小时前
第三章:Python3 之 字符串
开发语言·python·字符串·string
威联通网络存储5 小时前
告别掉帧与素材损毁:威联通 QuTS hero 如何重塑影视后期协同工作流
前端·网络·人工智能·python
Dxy12393102165 小时前
Python 根据列表中某字段排序:从基础到进阶
开发语言·windows·python
splage5 小时前
Java进阶——IO 流
java·开发语言·python
cliffordl5 小时前
设计模式(python)
python·设计模式
always_TT6 小时前
从Python_Java转学C语言需要注意什么?
java·c语言·python
2301_793804696 小时前
定时任务专家:Python Schedule库使用指南
jvm·数据库·python
穿越世纪的风尘7 小时前
【问题解决】No module named ‘_sqlite3‘
python·centos