日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > > AI科技大本營
[導(dǎo)讀]作者|黃偉呢來源|數(shù)據(jù)分析與統(tǒng)計(jì)學(xué)之美MongoDB是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。先來看看MySQL與MongoDB概念區(qū)別:今天的重點(diǎn),就是要為大家講述如何使用Python操作MongoDB數(shù)據(jù)庫。在正式進(jìn)行增刪改...

Python?操作?MongoDB?數(shù)據(jù)庫!


作者 |黃偉呢來源 |數(shù)據(jù)分析與統(tǒng)計(jì)學(xué)之美MongoDB是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。先來看看MySQL與MongoDB 概念區(qū)別:Python?操作?MongoDB?數(shù)據(jù)庫!


今天的重點(diǎn),就是要為大家講述如何使用Python操作MongoDB數(shù)據(jù)庫。在正式進(jìn)行增刪改查之前,我們需要先獲取一個(gè)叫做集合的東西,它就像是mysql數(shù)據(jù)庫中的表。# 安裝該庫后,這里才能導(dǎo)入
from pymongo import MongoClient
# 連接服務(wù)器
conn = MongoClient("localhost",27017)
# 連接數(shù)據(jù)庫
db = conn.mydb
# 獲取集合
collection = db.student

... 中間進(jìn)行一系列操作:增刪改查 ...

# 斷開連接【最后一步】
conn.close()
注意:Python 中寫MongoDB代碼和在MongoDB客戶端寫的代碼格式一樣。區(qū)別在于:在Python中寫MongoDB代碼,所有“鍵”都需要添加引號(hào)。

插入文檔

① 一次性插入一個(gè)文檔
collection.insert_one({"name""abc","age"19,"gender"1,"adress""北京","isDelete" 0})
② 一次性插入多個(gè)文檔
collection.insert_many([{"name""abc1","age"19,"gender"1,"adress""北京
"
,"isDelete"0},{"name""abc2","age"19,"gender"1,"adress""北京","isDelete"0}])

刪除文檔

# 刪除某條文檔
collection.remove({"name""lilei"})

# 不寫條件,代表全部刪除。不要輕易用
collection.remove()

修改文檔

# 修改文檔
collection.update({"name""lilei"},{"$set": {"age"25}})

查詢文檔

這里一共為大家列出了7條。
① 查詢部分文檔
res = collection.find({"age": {"$gte"19}})
for row in res:
print(row)
② 查詢所有文檔
res = collection.find()
for row in res:
print(row)
③ 統(tǒng)計(jì)查詢
res = collection.find().count()
print(res)
④ 根據(jù) id 查詢(這需要引入第三方庫)
from bson.objectid import ObjectId
res = collection.find({"_id":ObjectId("5cc506289e1d88c95465488e")})
print(res[0])
⑤ 升序排序
res = collection.find().sort("age")
for row in res:
print(row)
⑥ 降序排序(也需要引入第三方庫)
import pymongo
res = collection.find().sort("age",pymongo.DESCENDING)
for row in res:
print(row)
⑦ 分頁查詢
res = collection.find().limit(3).skip(5)
for row in res:
print(row)
Python?操作?MongoDB?數(shù)據(jù)庫!



本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉