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)
相关推荐
AAD5558889910 分钟前
基于Faster RCNN的暴力行为检测模型优化与实现_1
python
难得的我们16 分钟前
超越Python:下一步该学什么编程语言?
jvm·数据库·python
工程师老罗21 分钟前
Turtle库的用法
python
Sivan_Xin26 分钟前
拒绝 If-Else 屎山:利用适配器模式(Adapter)构建第三方登录的“防腐层”实战
linux·python·适配器模式
AI Echoes1 小时前
LangChain Runnable组件重试与回退机制降低程序错误率
人工智能·python·langchain·prompt·agent
AAD555888991 小时前
YOLO11-Seg+ContextGuided:智能交通流量估算与拥堵检测实战指南
python
rose and war1 小时前
python和jinja版本问题导致的访问报500
python·ios
小鸡吃米…1 小时前
机器学习 - 感知机(Perceptron)
人工智能·python·机器学习
毅炼2 小时前
Java 基础常见问题总结(1)
开发语言·python
风栖柳白杨2 小时前
【语音识别】Qwen3-ASR原理及部署
人工智能·python·语音识别·xcode·audiolm