python下mongodb數據增刪改查

# 導入 MongoClient 模塊
import pymongo

# 兩種方式
# 1. 傳入數據庫IP和端口號
mc = pymongo.MongoClient('127.0.0.1', 27017)

# 有密碼的連接
# 首先指定連接testdb數據庫
# db = mc.testdb

# 通過authenticate方法認證賬號密碼
# db.authenticate('username','password')

# 檢查是否連接成功,輸出以下結果表示連接成功
# print(mc.server_info())

# 指定操作數據庫的兩種方式
# 1. 獲取 testdb 數據庫,沒有則自動創建
# db = mc.testdb

# 2. 效果與上面 db = mc.testdb 相同
db = mc['testdb1']

# 打印出testdb數據庫下所有集合(表)
# print(db.collection_names())

# 指定操作集合的兩種方式
# 1. 獲取 test 集合,沒有則自動創建


# 2. 效果與 collection = db.test 相同
collection = db['test']

# 打印集合中一行數據


book = {
    'name': 'Python基礎',
    'author': '張三',
    'page': 80
}

# 向集合中插入一條記錄
collection.insert_one(book)

# 對於insert_many()方法,我們可以將數據以列表形式傳遞參數
book1 = {
    'name': 'Java基礎',
    'author': '李白',
    'page': 100
}

book2 = {
    'name': 'Java虛擬機',
    'author': '王五',
    'page': 100
}

# 創建 book_list 列表
book_list = [book1, book2]

# # 向集合中插入多條記錄
collection.insert_many(book_list)
#
# # 通過條件查詢一條記錄,如果不存在則返回None
res = collection.find_one({'author': '張三'})
print(res)
#
# # 通過條件查詢多條記錄,如果不存在則返回None
res = collection.find({'page': 100})
print(res)
#
# # 使用 find() 查詢會返回一個對象
# # 遍歷對象,並打印查詢結果


# # 查詢page大於50的記錄

res = collection.find({'page': {'$gt': 50}})
for r in res:
    print(r)
print(res)

#
# book = collection.find_one({'author': '張三'})
# book['page'] = 90
#
# 更新滿足條件{'author', '張三'}的第一條記錄
#   更新條件和更新的值
res = collection.update_one({'name': 'Python基礎'}, {'$set': {'page': 110}})

#
# # 更新返回結果是一個對象,我們可以調用matched_count和modified_count屬性分別獲得匹配的數據條數和影響的數據條數。
print(res.matched_count, res.modified_count)
#
# # 更新滿足條件 page>90 的所有記錄,page 字段自加 10
res = collection.update_many({'page': {'$gt': 90}}, {'$inc': {'page': 10}})
#
# # 打印更新匹配和影響的記錄數
print(res.matched_count, res.modified_count)
#
book3 = {'name': 'Python高級', 'author': '趙飛', 'page': 50}
#
# # upsert=True表示如果沒有滿足更新條件的記錄,則會將book3插入集合中
res = collection.update_one({'author': '趙飛'}, {'$set': book3}, upsert=True)     # update_one()  update_many()
print(res.matched_count, res.modified_count)
#
# # 查詢所有記錄,並遍歷打印出來
res = collection.find()
for r in res:
    print(r)
#
# # 刪除滿足條件的第一條記錄
result = collection.delete_one({'author': '張三'})
# # 同樣可以通過返回對象的 deleted_count 屬性查詢刪除的記錄數
# print(result.deleted_count)
#
# # 刪除滿足條件的所有記錄,以下爲刪除 page < 90 的記錄
result = collection.delete_many({'page': {'$lt': 90}})
print(result.deleted_count)
#
# # 查詢返回滿足條件的記錄然後刪除
result = collection.find_one_and_delete({'author': '王五'})  # 只刪除了一條數據。
print(result)

# # 統計查詢結果個數
# # 全部結果個數
# collection.find().count()
#
# # 滿足條件結果個數
# collection.find({'page': 100}).count()
#
# # 查詢結果按字段排序
# # 升序
# results = collection.find().sort('page', pymongo.ASCENDING)
# #
# # # 降序
# results = collection.find().sort('page', DESCENDING)
# #
# # # 下面查詢結果是按page升序排序,只返回第二條記錄及以後的兩條結果
# # results = collection.find().sort('page', ASCENDING).skip(1).limit(2)
# # print(results)
#
# # unique=True時,創建一個唯一索引,索引字段插入相同值時會自動報錯,默認爲False
# collection.create_index('page', unique=True)

# # 打印出已創建的索引
# print(collection.index_information())
#
# # # 刪除索引
# # collection.drop_index('page_1')
#
# # 刪除集合
collection.drop()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章