前言
往期回顧:
- Python進(jìn)階系列教程-- Python3 正則表達(dá)式(一)
- Python進(jìn)階系列教程-- Python3 CGI編程(二)
- Python進(jìn)階系列教程-- Python3 MySQL - mysql-connector 驅(qū)動(dòng)(三)
- Python進(jìn)階系列教程-- Python3 MySQL 數(shù)據(jù)庫(kù)連接 - PyMySQL 驅(qū)動(dòng)
- Python進(jìn)階系列教程-- Python3 網(wǎng)絡(luò)編程(五)
- Python進(jìn)階系列教程-- Python3 SMTP發(fā)送郵件(六)
- Python進(jìn)階系列教程-- Python3 多線(xiàn)程(七)
- Python進(jìn)階系列教程-- Python3 XML 解析(八)
- Python進(jìn)階系列教程-- Python3 JSON 數(shù)據(jù)解析(九)
- Python進(jìn)階系列教程-- Python3 日期和時(shí)間(十)
- Python進(jìn)階系列教程-- 內(nèi)置函數(shù)(十一)
MongoDB 是目前最流行的 NoSQL 數(shù)據(jù)庫(kù)之一,使用的數(shù)據(jù)類(lèi)型 BSON(類(lèi)似 JSON)。
MongoDB 數(shù)據(jù)庫(kù)安裝與介紹可以查看我們的 MongoDB 教程。
PyMongo
Python 要連接 MongoDB 需要 MongoDB 驅(qū)動(dòng),這里我們使用 PyMongo 驅(qū)動(dòng)來(lái)連接。
pip 安裝
pip 是一個(gè)通用的 Python 包管理工具,提供了對(duì) Python 包的查找、下載、安裝、卸載的功能。
安裝 pymongo:
$ python3 -m pip3 install pymongo
也可以指定安裝的版本:
$ python3 -m pip3 install pymongo==3.5.1
更新 pymongo 命令:
$ python3 -m pip3 install --upgrade pymongo
easy_install 安裝
舊版的 Python 可以使用 easy_install 來(lái)安裝,easy_install 也是 Python 包管理工具。
$ python -m easy_install pymongo
更新 pymongo 命令:
$ python -m easy_install -U pymongo
測(cè)試 PyMongo
接下來(lái)我們可以創(chuàng)建一個(gè)測(cè)試文件 demo_test_mongodb.py,代碼如下:
demo_test_mongodb.py 文件代碼:
#!/usr/bin/python3
import pymongo
執(zhí)行以上代碼文件,如果沒(méi)有出現(xiàn)錯(cuò)誤,表示安裝成功。
創(chuàng)建數(shù)據(jù)庫(kù)
創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)
創(chuàng)建數(shù)據(jù)庫(kù)需要使用 MongoClient 對(duì)象,并且指定連接的 URL 地址和要?jiǎng)?chuàng)建的數(shù)據(jù)庫(kù)名。
如下實(shí)例中,我們創(chuàng)建的數(shù)據(jù)庫(kù) demodb :
實(shí)例文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-490511.html
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["demodb "]
注意: 在 MongoDB 中,數(shù)據(jù)庫(kù)只有在內(nèi)容插入后才會(huì)創(chuàng)建! 就是說(shuō),數(shù)據(jù)庫(kù)創(chuàng)建后要?jiǎng)?chuàng)建集合(數(shù)據(jù)表)并插入一個(gè)文檔(記錄),數(shù)據(jù)庫(kù)才會(huì)真正創(chuàng)建。
判斷數(shù)據(jù)庫(kù)是否已存在
我們可以讀取 MongoDB 中的所有數(shù)據(jù)庫(kù),并判斷指定的數(shù)據(jù)庫(kù)是否存在:
實(shí)例
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
dblist = myclient.list_database_names()
# dblist = myclient.database_names()
if "demodb" in dblist:
print("數(shù)據(jù)庫(kù)已存在!")
注意:database_names 在最新版本的 Python 中已廢棄,Python3.7+ 之后的版本改為了 list_database_names()。
創(chuàng)建集合
MongoDB 中的集合類(lèi)似 SQL 的表。
創(chuàng)建一個(gè)集合
MongoDB 使用數(shù)據(jù)庫(kù)對(duì)象來(lái)創(chuàng)建集合,實(shí)例如下:
實(shí)例
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["demodb"]
mycol = mydb["sites"]
注意: 在 MongoDB 中,集合只有在內(nèi)容插入后才會(huì)創(chuàng)建! 就是說(shuō),創(chuàng)建集合(數(shù)據(jù)表)后要再插入一個(gè)文檔(記錄),集合才會(huì)真正創(chuàng)建。
判斷集合是否已存在
我們可以讀取 MongoDB 數(shù)據(jù)庫(kù)中的所有集合,并判斷指定的集合是否存在:
實(shí)例
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['demodb']
collist = mydb. list_collection_names()
# collist = mydb.collection_names()
if "sites" in collist: # 判斷 sites 集合是否存在
print("集合已存在!")
注意:collection_names 在最新版本的 Python 中已廢棄,Python3.7+ 之后的版本改為了 list_collection_names()。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-490511.html
到了這里,關(guān)于【Python】Python進(jìn)階系列教程-- MongoDB(十二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!