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)
相关推荐
绍兴贝贝17 分钟前
代码随想录算法训练营第四十六天|LC647.回文子串|LC516.最长回文子序列|动态规划总结
数据结构·人工智能·python·算法·动态规划·力扣
___波子 Pro Max.1 小时前
Python参数解析默认True变False
python
橙露1 小时前
面向对象编程思想:Java 与 Python 的封装、继承与多态对比分析
java·开发语言·python
Jia ming1 小时前
《智能法官软件项目》—法条检索模块
python·教学·案例·智能法官软件
LitchiCheng2 小时前
Mujoco 如何添加 Apriltag 并获得相机视野进行识别
人工智能·python·开源
一切尽在,你来2 小时前
LangGraph快速入门
人工智能·python·langchain·ai编程
啊哈哈121382 小时前
Python基本语法复盘笔记1(输入输出/字符串/列表)
开发语言·笔记·python
SCLchuck3 小时前
人工智能-概率密度估计
人工智能·python·概率论·概率密度估计
摩拜芯城IC3 小时前
PSD835G2-90UI 芯片参数资料意法半导体Flash PSD可编程系统微芯片
python·5g
清水白石0083 小时前
Python 缓存机制深度实战:从零打造带过期时间的记忆化装饰器
python·spring·缓存