pymongo基礎操作
阿新 • • 發佈:2020-09-06
Pymongo 基礎操作
mongo資料庫連結
import pymongo def test(): client = pymongo.MongoClient("127.0.0.1:27017") db = client['test_db'] collection = db['test_collection'] # 也可以這樣寫 collection = pymongo.MongoClient("127.0.0.1:27017")['test_db']['test_collection'] if __name__ == '__main__': test()
插入資料
import pymongo def test(): collection = pymongo.MongoClient("127.0.0.1:27017")['test_db']['test_collection'] # 用dict組織資料比較方便 # _id 是mongo預設生成的主鍵,同時會建立索引,也可以在程式碼中複製(唯一),也會自動建立索引 # 目前三個插入資料的介面,insert_one insert_many insert insert_one = { '_id': 1, 'data': 'test insert one' } insert_many = [{'_id': 3, 'data': 'test insert many'}, {'_id': 4, 'data': 'test insert many'}] insert = { '_id': 5, 'data': 'test insert' } # insert_one # 插入時可以返回 id collection.insert_one(insert_one).inserted_id # insert_many collection.insert_many(insert_many).inserted_ids # insert # 在使用insert時,會有警告 insert is deprecated. Use insert_one or insert_many instead. # 所以推薦使用insert_one 或 insert_many collection.insert(insert) if __name__ == '__main__': test()
刪除資料
import pymongo def test(): collection = pymongo.MongoClient("127.0.0.1:27017")['test_db']['test_collection'] # 目前兩個刪除資料的介面,delete_one delete_many delete_one = {'_id': 1} # $in 見文章末尾的$高階用法 delete_many = {'_id': {'$in': [3, 4]}} # delete_one collection.delete_one(delete_one) # delete_many collection.delete_many(delete_many) if __name__ == '__main__': test()
更新操作
import pymongo
def test():
collection = pymongo.MongoClient("127.0.0.1:27017")['test_db']['test_collection']
# 目前兩個刪除資料的介面,update_one update_many
# 更新條件,一條資料
condition = {'_id': 5}
update_data = {'$set': {'update': 'update_test'}}
collection.update_one(condition, update_data)
# 更新條件,多條資料, 此操作只能使用 $set $inc 等操作符
condition = {'_id': {'$gt': 2}}
update_data = {'$set': {'update_many': 'test update many'}}
collection.update_many(condition, update_data)
# collection.update_one(condition, update_data,upsert=True)
# 可以實現upsert的操作,還可以用$setOnInsert 保證某些資料只在插入時有效
if __name__ == '__main__':
test()
查詢操作
import pymongo
def test():
collection = pymongo.MongoClient("127.0.0.1:27017")['test_db']['test_collection']
# 目前兩個刪除資料的介面 find find_one
# 查詢所有檔案, 返回值是Cursor型別, 需要遍歷取元素,每個元素都是字典型
# 條件查詢 condition = {'_id': {'$gt': 2}} 表示 _id > 2
cursor = collection.find({})
cursor = collection.find({'_id': {'$gt': 2}})
# 單條查詢, 返回值是字典型
doc = collection.find_one({'_id': 3})
if __name__ == '__main__':
test()
索引
import pymongo
from pymongo import IndexModel
def test():
collection = pymongo.MongoClient("127.0.0.1:27017")['test_db']['test_collection']
# 目前兩個刪除資料的介面 find find_one
# 建立一個欄位的索引,1 代表升序, unique=True 表示是否唯一
collection.create_index([('name', 1)], unique=True)
# 建立多個欄位的索引
index1 = IndexModel([("index", pymongo.DESCENDING), ("one", pymongo.ASCENDING)], name="index_one") # 複合索引
index2 = IndexModel([("index2", pymongo.DESCENDING)])
collection.create_indexes([index1, index2])
if __name__ == '__main__':
test()
$ 高階用法
數學比較符
$lt 小於 {'_id': {'$lt': 1}}
$lte 小於等於 {'_id': {'$lte': 1}}
$gt 大於 {'_id': {'$gt': 1}}
$gte 大於等於 {'_id': {'$gte': 1}}
$ne 不等於 {'_id': {'$ne': 1}}
查詢關鍵字
$in 在範圍內 {'_id': {'$in': [1, 2]}}
$nin 不在範圍內 {'_id': {'$nin': [1, 2]}}
$or or {'$or':[{'_id':19},{'name':'test'}]}
$and and{'$and':[{'_id':{'$lte':2}},{'_id':{'$gte':1}}]}
$all 與$in類似,但有區別 {'data':{'$all':[1,2,3]}} 要求data屬性包含所有的[1,2,3]條件
修改器
$set 強制替換覆蓋
$unset 強制刪除欄位
$setOnInsert upsert時強制指定插入欄位
$inc 引用增加 例:原欄位+1