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实现数据库两个表之间的更新操作(模糊匹配)示例
半吊子的程序狗2024-10-28 15:27
相关推荐
2601_9622982713 小时前
Python自动化脚本的编写01AIFQuant13 小时前
Python实时外汇行情接入实战:WebSocket与REST K线查询babe小鑫14 小时前
生物统计学专业校招:SAS、R、Python学习顺序实用指南vx_Biye_Design14 小时前
springboot游泳馆系统93765-计算机课程设计、毕业设计固定资产管理系统软件14 小时前
该去哪里找专业靠谱的智慧智能设备固定资产管理系统?ctlover14 小时前
LangChain 概述专业程序开发源14 小时前
springbootLivehouse票务系统-计算机课程设计、毕业设计李高钢14 小时前
Python Django 框架入门:从零搭建你的第一个 Web 应用2601_9669496515 小时前
从轮询到策略消费:量化系统如何高效处理五档盘口数据萧鼎15 小时前
2026实战:用 accelerate 库加速 PyTorch 训练,核心语法与避坑指南