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)
相关推荐
Data_agent16 分钟前
1688按图搜索1688商品(拍立淘)API ,Python请求示例
爬虫·python·算法·图搜索算法
吃好喝好玩好睡好33 分钟前
OpenHarmony混合开发实战指南
c语言·python·flutter·vr·visual studio
white-persist42 分钟前
【攻防世界】reverse | re1-100 详细题解 WP
c语言·开发语言·网络·汇编·python·算法·网络安全
CHANG_THE_WORLD44 分钟前
Python 中的循环结构详解
开发语言·python·c#
quikai19811 小时前
python练习第一组
开发语言·python
谷粒.1 小时前
测试数据管理难题的7种破解方案
运维·开发语言·网络·人工智能·python
寒山李白2 小时前
关于Python版本与supervisor版本的兼容性
windows·python·supervisord
梨落秋霜2 小时前
Python入门篇【基础语法】
开发语言·python
ada7_2 小时前
LeetCode(python)——543.二叉树的直径
数据结构·python·算法·leetcode·职场和发展
小白学大数据2 小时前
Python 多线程爬取社交媒体品牌反馈数据
开发语言·python·媒体