本文實例講述了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é)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
