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)
相关推荐
python猿3 分钟前
打卡Python王者归来--第30天
开发语言·python
2401_8318249618 分钟前
为你的Python脚本添加图形界面(GUI)
jvm·数据库·python
2401_8796938720 分钟前
用Pygame开发你的第一个小游戏
jvm·数据库·python
用户03321266636728 分钟前
使用 Python 查找并高亮 Word 文档中的文本
python
xushichao198929 分钟前
实战:用OpenCV和Python进行人脸识别
jvm·数据库·python
yy我不解释1 小时前
关于comfyui的mmaudio音频生成插件时时间不一致问题(三)
开发语言·python·ai作画·音视频·comfyui
冗量1 小时前
langchain的学习路径
python·langchain
love530love1 小时前
不用聊天软件 OpenClaw 手机浏览器远程访问控制:Tailscale 配置、设备配对与常见问题全解
人工智能·windows·python·智能手机·tailscale·openclaw·远程访问控制
站大爷IP1 小时前
Python 合并 PDF 文件(批量处理方法)
python
2501_945423541 小时前
工具、测试与部署
jvm·数据库·python