【MongoDB詳細使用教程】一、Mac安裝MongoDB
【MongoDB詳細使用教程】二、MongoDB基本操作
【MongoDB詳細使用教程】三、高級查詢
【MongoDB詳細使用教程】四、python操作MongoDB
【MongoDB詳細使用教程】五、MongoDB的數據庫管理
使用第三方庫pymongo來實現python對MongoDB的操作
pymongo官方文檔:https://api.mongodb.com/python/current/tutorial.html
1、安裝pymongo
pip install 安裝pymongo
2、連接數據庫
import pymongo
client = pymongo.MongoClient('localhost', 27017) # 連接服務器,需要先開啟服務
db = client['mymongo'] # 選擇數據庫
data = db.students.find() # 查詢數據,返回一個游標,通過對游標進行遍歷來獲取每條數據
print(db)
print(data)
# 對查詢到的數據進行遍歷,每一項為一個dict
for i in data:
print(i, type(i))
返回結果:
MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True)
Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'mymongo')
<pymongo.cursor.Cursor object at 0x1058e57f0>
{'_id': ObjectId('5db642b30f98841018f76965'), 'name': 'chen', 'age': 18.0, 'grade': '一年級'} <class 'dict'>
{'_id': ObjectId('5db642bc0f98841018f76966'), 'name': 'wang', 'age': 19.0, 'grade': '二年級'} <class 'dict'>
{'_id': ObjectId('5db653920f98841018f7696b'), 'name': 'xu', 'age': 20.0, 'grade': '三年級', 'text': ['女', '研究員']} <class 'dict'>
{'_id': ObjectId('5db654660f98841018f7696c'), 'name': 'ma', 'age': 20.0, 'grade': '二年級', 'text': ['女', '副教授', '副處長']} <class 'dict'>
{'_id': ObjectId('5db68d190f98841018f76970'), 'name': 'cheng', 'age': 21.0, 'grade': '四年級'} <class 'dict'>
{'_id': ObjectId('5db68f6c0f98841018f76971'), 'name': 'cheng', 'age': 22.0, 'grade': '五年級'} <class 'dict'>
3、操作數據庫
python操作mysql和oracle都是通過直接執行sql來完成,
而對MongoDB的操作是通過pymongo提供的方法來完成的。
本節不再把語法單獨提出,所有"students"字樣均為集合名。
3.1、查
data_all = db.students.find() # 查詢全部
data_lim = db.students.find().limit(1) # 返回第一條
data_the = db.students.find({"name": "xu"}) # 條件查詢(結果只有1條匹配)
data_one = db.students.find_one() # 查詢一條
print(data_all, type(data_all))
print(data_lim, type(data_lim))
print(data_the, type(data_the))
print(data_one, type(data_one))
雖然后3個方法得到的數據都是1條,但只有使用.find_one()時會返回dict,其余返回的都是Cursor(游標),需要遍歷才可以得到具體數據。
<pymongo.cursor.Cursor object at 0x105a04780> <class 'pymongo.cursor.Cursor'>
<pymongo.cursor.Cursor object at 0x105a047f0> <class 'pymongo.cursor.Cursor'>
<pymongo.cursor.Cursor object at 0x105a04860> <class 'pymongo.cursor.Cursor'>
{'_id': ObjectId('5db642b30f98841018f76965'), 'name': 'chen', 'age': 18.0, 'grade': '一年級'} <class 'dict'>
3.2、增
# 插入單條
db.students.insert_one({"name": "zuo", "age": 40, "grate": "九年級"})
# 插入多條
many_data = [{"name": "ding", "age": 40, "grate": "九年級"},
{"name": "liao", "age": 42, "grate": "十年級"},
{"name": "zhao", "age": 35, "grate": "九年級"}]
db.students.insert_many(many_data)
3.3、改
# 修改單條
db.students.update_one(filter={"name": "zuo"}, update={"$set": {"grate": "十年級"}})
# 修改全部匹配項
db.students.update_many(filter={"name": "zuo"}, update={"$set": {"grate": "十年級"}})
# filter后為條件,update后為修改后值,其中$set為固定語法。
3.4、刪
# 刪除單條
db.students.delete_one({}) # 刪除全部數據的第一條
db.students.delete_one({"name": "zuo"}) # 刪除匹配項的第一條
# 刪除多條
db.students.delete_many({"name":"zuo"}) # 刪除集合中的全部數據
db.students.delete_many({"name":"zuo"}) # 刪除全部匹配項