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
相关推荐
web3.08889997 小时前
微店商品详情API实用知乎的哥廷根数学学派7 小时前
基于数据驱动的自适应正交小波基优化算法(Python)sunfove7 小时前
将 Python 仿真工具部署并嵌入个人博客Learner7 小时前
Python类2501_941329728 小时前
门及其组件定位识别_YOLO13-C3k2-PoolingFormer改进模型研究Ancelin安心8 小时前
kali-dirsearch的使用努力学习的小洋8 小时前
Python训练打卡Day5离散特征的处理-独热编码Sherry Wangs9 小时前
【ML】机器学习进阶X1A0RAN9 小时前
python 借助 paramiko 库执行 SSH命令报错:input is not a terminal 解决方式百***78759 小时前
Grok-4.1与GPT-5.2深度对比:技术差异、适用场景及Python集成指南