Python操作Mongodb數(shù)據(jù)庫的方法小結(jié)
本文實例講述了Python操作Mongodb數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
一 導(dǎo)入 pymongo
from pymongo import MongoClient
二 連接服務(wù)器 端口號 27017
連接MongoDB
連接MongoDB我們需要使用PyMongo庫里面的MongoClient,一般來說傳入MongoDB的IP及端口即可,第一個參數(shù)為地址host,第二個參數(shù)為端口port,端口如果不傳默認(rèn)是27017。
conn = MongoClient("localhost")
MongoClient(host='127.0.0.1',port=27017)
三 連接數(shù)據(jù)庫
db = conn.數(shù)據(jù)庫名稱
連接集合
collection = db[collection_name]
or
collection = db.collection_name
查看全部聚集名稱
db.collection_names()
四 插入數(shù)據(jù)
(1) 插入一條數(shù)據(jù)
db.user.insert({"name":"夏利剛","age":18,"hobby":"學(xué)習(xí)"})
(2) 插入多條數(shù)據(jù)
db.user.insert([{"name":"夏利剛","age":18,"hobby":"學(xué)習(xí)"},{"name":"xxxoo","age":48,"hobby":"學(xué)習(xí)"}]
(3) 在3.x以上 建議 使用
insert_one 插入一條數(shù)據(jù)
insert_many() 插入多條數(shù)據(jù)
(4) 返回 id 使用insert_one()
data.inserted_id data.inserted_ids
五 查詢數(shù)據(jù)
(1) 查詢所有
db.user.find()
#帶條件的查詢
# data = db.user.find({"name":"周日"})
# print(data) #返回result類似一個迭代器 可以使用 next方法 一個一個 的取出來
# print(next(data)) #取出一條數(shù)據(jù)
(2) 查詢一條
db.user.find_one()
(3) 帶條件查詢
db.user.find({"name":"張三"})
(4) 查詢 id
from bson.objectid import ObjectId*#用于ID查詢
data = db.user.find({"_id":ObjectId("59a2d304b961661b209f8da1")})
(5) 模糊查詢
(1)
{"name":{'$regex':"張"}}
(2)
import re {'xxx':re.compile('xxx')}
六 sort limit count skip
(1) sort 排序
年齡 大于10
data = db.user.find({"age":{"$gt":10}}).sort("age",-1) #年齡 升序 查詢 pymongo.ASCENDING --升序
data = db.user.find({"age":{"$gt":10}}).sort("age",1) #年齡 降序 查詢 pymongo.DESCENDING --降序
(2) limit 取值
取三條數(shù)據(jù)
db.user.find().limit(3)
data = db.user.find({"age":{"$gt":10}}).sort("age",-1).limit(3)
(3) count 統(tǒng)計數(shù)據(jù)條數(shù)
db.user.find().count()
(4) skip 從第幾條數(shù)據(jù)開始取
db.user.find().skip(2)
七 update 修改
update()方法其實也是官方不推薦使用的方法,在這里也分了update_one()方法和update_many()方法,用法更加嚴(yán)格,
(1) update()
db.user.update({"name":"張三"},{"$set":{"age":25}})
db.user.update({"name":"張三"},{"$inc":{"age":25}})
(2) update_one() 第一條符合條件的數(shù)據(jù)進(jìn)行更新
db.user.update_one({"name":"張三"},{"$set":{"age":99}})
(3) update_many() 將所有符合條件的數(shù)據(jù)都更新
db.user.update_many({"name":"張三"},{"$set":{"age":91}})
(4) 其返回結(jié)果是UpdateResult類型,然后調(diào)用matched_count和modified_count屬性分別可以獲得匹配的數(shù)據(jù)條數(shù)和影響的數(shù)據(jù)條數(shù)。
print(result.matched_count, result.modified_count)
八 remove 刪除
刪除操作比較簡單,直接調(diào)用remove()方法指定刪除的條件即可,符合條件的所有數(shù)據(jù)均會被刪除,
(1) 刪除 張三
collection.remove({"name":"lilei"})
(2) 全部刪除
collection.remove()
(3) 依然存在兩個新的推薦方法,delete_one()和delete_many()方法,示例如下:
delete_one()即刪除第一條符合條件的數(shù)據(jù)
collection.delete_one({"name":" Kevin"})
delete_many()即刪除所有符合條件的數(shù)據(jù),返回結(jié)果是DeleteResult類型
collection.delete_many({"age": {$lt:25}})
(4) 可以調(diào)用deleted_count屬性獲取刪除的數(shù)據(jù)條數(shù)。
result.deleted_count
九 關(guān)閉連接
conn.close()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
使用Python給頭像加上圣誕帽或圣誕老人小圖標(biāo)附源碼
圣誕的到來給大家?guī)硐矏偅裉焓フQ老人給大家送一頂圣誕帽,今天小編通過代碼給大家分享使用Python給頭像加上圣誕帽或圣誕老人小圖標(biāo)附源碼,需要的朋友一起看看吧2019-12-12
詳解python如何正確使用時間戳,日期,時間,時區(qū)
這篇文章主要為大家介紹了如何在python中正確使用時間戳,日期,時間,時區(qū),文中通過簡單的示例進(jìn)行了詳細(xì)介紹,希望對大家有一定的幫助2024-11-11
關(guān)于Python中zipfile壓縮包模塊的使用
這篇文章主要介紹了關(guān)于Python中zipfile壓縮包模塊的使用,zipfile?模塊提供了創(chuàng)建、讀取、寫入、添加及列出?ZIP?文件的工具,本文做一個簡單的總結(jié),需要的朋友可以參考下2023-04-04

