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
相关推荐
前端摸鱼匠24 分钟前
YOLOv8 环境配置全攻略:Python、PyTorch 与 CUDA 的和谐共生WangYaolove131431 分钟前
基于python的在线水果销售系统(源码+文档)AALoveTouch38 分钟前
大麦网协议分析ZH15455891311 小时前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现xcLeigh1 小时前
Python入门:Python3 requests模块全面学习教程xcLeigh1 小时前
Python入门:Python3 statistics模块全面学习教程YongCheng_Liang2 小时前
从零开始学 Python:自动化 / 运维开发实战(核心库 + 3 大实战场景)鸽芷咕2 小时前
为什么越来越多开发者转向 CANN 仓库中的 Python 自动化方案?秋邱2 小时前
用 Python 写出 C++ 的性能?用CANN中PyPTO 算子开发硬核上手指南wazmlp0018873693 小时前
python第三次作业