【MongoDB詳細使用教程】4、python操做MongoDB

【MongoDB詳細使用教程】1、Mac安裝MongoDB
【MongoDB詳細使用教程】2、MongoDB基本操做
【MongoDB詳細使用教程】3、高級查詢
【MongoDB詳細使用教程】4、python操做MongoDB
【MongoDB詳細使用教程】5、MongoDB的數據庫管理python

使用第三方庫pymongo來實現python對MongoDB的操做
pymongo官方文檔:https://api.mongodb.com/python/current/tutorial.htmlmysql

一、安裝pymongo

pip install 安裝pymongo

二、鏈接數據庫

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))

返回結果:sql

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'>

三、操做數據庫

python操做mysql和oracle都是經過直接執行sql來完成,
而對MongoDB的操做是經過pymongo提供的方法來完成的。mongodb

本節再也不把語法單獨提出,全部"students"字樣均爲集合名。數據庫

3.一、查

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(遊標),須要遍歷才能夠獲得具體數據。api

<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.二、增

# 插入單條
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.三、改

# 修改單條
db.students.update_one(filter={"name": "zuo"}, update={"$set": {"grate": "十年級"}})

# 修改所有匹配項
db.students.update_many(filter={"name": "zuo"}, update={"$set": {"grate": "十年級"}})


# filter後爲條件,update後爲修改後值,其中$set爲固定語法。

3.四、刪

# 刪除單條
db.students.delete_one({})  # 刪除所有數據的第一條
db.students.delete_one({"name": "zuo"}) # 刪除匹配項的第一條

# 刪除多條
db.students.delete_many({"name":"zuo"}) # 刪除集合中的所有數據
db.students.delete_many({"name":"zuo"}) # 刪除所有匹配項
相關文章
相關標籤/搜索