目录

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)
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
Aerkui1 小时前
Python标准库-copy
开发语言·python
Try,多训练1 小时前
Pytorch查看神经网络结构和参数量
人工智能·pytorch·python
hweiyu002 小时前
Python从入门到精通全套视频教程免费
开发语言·python
安迪小宝5 小时前
python基础语法10-异常处理
服务器·开发语言·python
Kylin5245 小时前
Java实验二
java·开发语言·python
深度学习lover5 小时前
<数据集>苹果识别数据集<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果识别
DreamNotOver6 小时前
自动提取pdf公式 ➕ 输出 LaTeX
python·pdf·gui·提取公式
Jacky-0088 小时前
python reportlab模块----操作PDF文件
python
chenchihwen8 小时前
Python合并多个pdf
开发语言·python·pdf
小怪瘦799 小时前
IDEA :物联网ThingsBoard-gateway配置,运行Python版本,连接thingsboard,接入 MQTT 设备
python·物联网·gateway·idea