Python與MongoDB交互的代碼實(shí)現(xiàn)
Python與MongoDB的交互通常通過pymongo庫來實(shí)現(xiàn)。pymongo是一個(gè)官方的Python驅(qū)動程序,用于與MongoDB數(shù)據(jù)庫進(jìn)行交互。以下是一個(gè)簡單的示例,展示了如何使用pymongo來連接到MongoDB數(shù)據(jù)庫,執(zhí)行一些基本的數(shù)據(jù)庫操作(如插入、查詢、更新和刪除文檔)。
安裝pymongo
pip install pymongo
Python連接Mongodb
from pymongo import MongoClient # 創(chuàng)建一個(gè)MongoClient對象,該對象用于連接Mongodb數(shù)據(jù)庫服務(wù)器 client = MongoClient('mongodb://admin:password@localhost:27017/')
向數(shù)據(jù)庫中添加數(shù)據(jù)
from mongo_db import client # client.school.teacher.insert_one({"name": "露娜"}) # client.school.teacher.insert_many([{"name":"蘭陵王"}, {"name": "百里玄策"}]) client.school.teacher.find({})
查詢數(shù)據(jù)
from mongo_db import client try: teachers = client.school.teacher.find() # print(teachers) for one in teachers: print(one["_id"],one["name"]) # 有條件查詢 teacher = client.school.teacher.find_one({"name":"蘭陵王"}) print(teacher["_id"],teacher["name"]) except Exception as e: print(e)
更新修改數(shù)據(jù)
from mongo_db import client try: # 全表修改,表中無此列時(shí)會增加此列 # client.school.teacher.update_many({}, {'$set': {"role":["班主任"]}}) # client.school.teacher.update_one({"name": "露娜"},{"$set":{"sex": "女"}}) # push追加(列表) client.school.teacher.update_one({"name": "露娜"},{"$push":{"role": "年級王主任"}}) except Exception as e: print(e)
刪除數(shù)據(jù)
from mongo_db import client try: # 有條件查詢 # client.school.teacher.delete_one({"name": "露娜"}) # 刪除多條數(shù)據(jù) client.school.teacher.delete_many({}) except Exception as e: print(e)
索引
創(chuàng)建索引以優(yōu)化查詢性能。
collection.create_index([("name", pymongo.ASCENDING)])
聚合框架
使用聚合框架進(jìn)行復(fù)雜的數(shù)據(jù)聚合操作
pipeline = [ {"$match": {"age": {"$gt": 25}}}, {"$group": {"_id": "$city", "count": {"$sum": 1}}} ] results = collection.aggregate(pipeline) for result in results: print(result)
事務(wù)
從MongoDB 4.0開始,支持多文檔事務(wù),確保數(shù)據(jù)的一致性和完整性。
with client.start_session() as session: with session.start_transaction(): collection.update_one({"name": "John"}, {"$set": {"age": 32}}, session=session) collection.update_one({"name": "Alice"}, {"$set": {"age": 26}}, session=session) session.commit_transaction()
關(guān)閉連接
client.close()
注意事項(xiàng)
在使用pymongo進(jìn)行MongoDB操作時(shí),需要確保MongoDB服務(wù)已經(jīng)啟動并且網(wǎng)絡(luò)連接正常。
在進(jìn)行數(shù)據(jù)插入、更新和刪除操作時(shí),需要注意數(shù)據(jù)的完整性和一致性。
MongoDB的集合是動態(tài)創(chuàng)建的,即當(dāng)向一個(gè)不存在的集合插入數(shù)據(jù)時(shí),MongoDB會自動創(chuàng)建該集合。
到此這篇關(guān)于Python與MongoDB交互的代碼實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python與MongoDB交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python斯皮爾曼spearman相關(guān)性分析實(shí)例
這篇文章主要為大家介紹了python斯皮爾曼spearman相關(guān)性分析實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02python中實(shí)現(xiàn)精確的浮點(diǎn)數(shù)運(yùn)算詳解
計(jì)算機(jī)智能處理可數(shù)集合的運(yùn)算,但是全體實(shí)數(shù)是不可數(shù)的,所以計(jì)算機(jī)只能用一些奇怪的方法來擬合他,于是就產(chǎn)生了浮點(diǎn)數(shù)。下面這篇文章主要給大家介紹了關(guān)于python中實(shí)現(xiàn)精確浮點(diǎn)數(shù)運(yùn)算的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(八):XML生成與解析(DOM、ElementTree)
DOM是Document Object Model的簡稱,XML 文檔的高級樹型表示。該模型并非只針對 Python,而是一種普通XML 模型。Python 的 DOM 包是基于 SAX 構(gòu)建的,并且包括在 Python 2.0 的標(biāo)準(zhǔn) XML 支持里2014-06-06