pymongo的簡單使用

  • 2020 年 1 月 16 日
  • 筆記

pymongo的使用

首先安裝:

pip install pymongo

安裝好了使用

import pymongo    # 鏈接mongodb,得到一個mongoclient的客戶端對象  client = pymongo.MongoClient()    # 指定資料庫  db = client.test  db = client["test"]   # 這兩種方式都可以指定資料庫,如果沒有該資料庫的話,會自行創建  # 如果了解面向對象的一些魔法(內置)方法的話,大概能夠知道client對應的類,肯定重寫__getattr__,和__getitem__方法    # 指定集合  collection = db.users  collection = db["users"]   # 同樣這兩種方法都可以指定到集合,不存在會自行創建

知道了資料庫和集合,下面就是對文檔的操作了

# 接著上面的內容    # 插入文檔  result = collection.insert({"name":"zhuchunyu","age":22})  # 返回值就是一個對象,輸出效果為ObjectId('5cde10c2e200928c8fa29315')  result = collection.insert([{"name":"zhanghao","age":22},{"name":"dsb","age":22}])  # 上面這行程式碼是插入多個文檔,返回值就是一個list,裡面元素就是一個一個的對象  # insert這個方法可以插入多條文檔,也可以插入單條文檔    # 插入單條文檔  result = collection.insert_one({"name":"zhuchunyu","age":22})  result.inserted_id  # 返回值也是一個對象,但是這個對象和上面返回值是不一樣的,自己可以type(result)看看    # 插入多條文檔  results = collection.insert_many([{"name":"wuyang","age":21},{"name":"yangjingpeng","age":22}])  # 返回值也是一個對象      # 查詢文檔  # 大概就是兩個方法,find(),find_one()    # find()方法,返回值為一個對象  result = collection.find({})   # 將集合里的所有文檔都查詢出來  result = collection.find({"name":"zhuyu"})  # 查詢符合參數一的條件的文檔  # 可以通過for循環將文檔依次列印出來  for i in result:      print(i)    # find_one(),查詢一條文檔,返回值就是一個字典,裡面就是文檔內容  result = collection.find_one({})   # 只返回符合條件的一個文檔數據  # 其實它最終還是調用的是find()方法,通過limit拿到一條文檔數據    # 這兩個方法大概能知道做什麼事了,下面繼續看方法里的參數,這次是重點  # 不管是find_one()還是find(),他們最終執行的就是dind()這個方法,我們看這個方法的參數就行了    # find()這個方法,最終返回的就是Cursor這個類的對象,最好是先自己看看源碼,我們繼續看這個類  # 我們主要看這兩個參數 filter,和projection    # filter就是我們的查詢條件,projection就是指定返迴文檔的哪些欄位數據  # 有這樣格式文檔數據的集合,文檔不止下面這一條,有很多  {         _id: 4,         name: "xi",         age: 34,         type: 2,         status: "D",         favorites: { artist: "Chagall", food: "chocolate" },         finished: [ 5, 11 ],         badges: [ "red", "black" ],         points: [            { points: 53, bonus: 15 },            { points: 51, bonus: 15 }         ]  }    # 當前這個集合的名字賦值給了collection這個變數  # 查詢出name欄位為"xi"的所有文檔  res = collection.find({"name":"xi"})    # 查詢age欄位大於20的所有文檔數據  res = collection.find({"age":{"$gt":20}})    # 查詢status欄位為"D",且age欄位小於50的所有文檔  res = collection.find({"status":"D","age":{"$lt":50}})    # 查詢status欄位為"D",或者age欄位小於50的所有文檔  res = collection.find({"$or":[{"status":"D"},{"age":{"$lt":50}}]})    # 查詢age欄位大於30小於50的文檔  res = collection.find({"age": {"$lt": 50, "$gt": 30}})    # 查詢finished欄位數組有5這個元素的文檔  res = collection.find({"finished": 5})    # 查詢favorites欄位里的文檔artist欄位為"Chagall"的文檔  res = collection.find({"favorites.artist": "Chagall"})    # 查詢points欄位里的文檔欄位points為53,並且bonus欄位為15的文檔  res = collection.find({"points.points": 53, "points.bonus": 15})    # projection就是指定返迴文檔的哪些欄位數據  # 這是第二個參數,傳遞一個字典,key就是欄位名,value就是0或1,0代表不需要,1代表需要  # 比如上面那個例子,我想查詢name欄位為"xi",且我只需要name,age這兩個欄位  res = collection.find({"name":"xi"},{"name":1,"age":"1","_id":0})    #注意:find()返回值是Cursor這個類的對象,res可以繼續使用該對象里的方法,我們通過print,或者for循環這個對象,只是觸發了它裡面的一些內置方法。    # 更新文檔  # 大概就是update,update_one,update_many    # update(),至少傳遞兩個參數,參數一就是filter(篩選條件),參數二就是更新後的文檔  # 比如我有這樣的一條文檔{"name":"zhuyu","age":22...}  # 我想把這條文檔的age欄位改為23,其他的欄位數據不發生變化  res_dict = collection.find_one({"name":"zhuyu"})  res_dict["age"] = 23  collection.update({"name":"zhuyu"},res_dict)  # 對了,就算根據篩選條件得出的結果有多條,也只會更新其中的一條文檔    # update_one,也是至少傳遞兩個參數,具體的參數可以去看源碼,他只會將參數二的給的欄位的值進行更新,不會像update那樣,整條數據都進行更新  # 還是繼續上面那個例子:將age欄位改為23  collection.update_one({"name":"zhuyu"},{"$set":{"age":23}})    # update_many,更新多條文檔      # 刪除文檔,參數至少一個,就是filter(篩選條件)  # delete_one() 刪除一條文檔  # delete_many()刪除多條文檔