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
相关推荐
Flittly4 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)千寻girling9 小时前
一份不可多得的 《 Django 》 零基础入门教程databook12 小时前
探索视觉的边界:用 Manim 重现有趣的知觉错觉明月_清风13 小时前
Python 性能微观世界:列表推导式 vs for 循环明月_清风13 小时前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践helloweilei1 天前
python 抽象基类用户8356290780511 天前
Python 实现 PPT 转 HTMLzone77391 天前
004:RAG 入门-LangChain读取PDFzone77391 天前
005:RAG 入门-LangChain读取表格数据树獭非懒2 天前
AI大模型小白手册|Embedding 与向量数据库