1. 程式人生 > >【python】詳解pymongo庫的使用

【python】詳解pymongo庫的使用

1、連線mongodb,建立client

傳入 ip 值或者 localhost ,埠是27017

import pymongo
from pymongo import MongoClient
client = MongoClient()
#連線到本地
client = MongoClient('mongodb://localhost:27017/')
#或者
client = MongoClient('localhost', 27017)

#如果是遠端資料庫,將 localhost 改為 ip 地址即可
client = MongoClient('172.169.11.51',27017)

2、連線資料庫

db = client.Aicoin_db
db
Out[2]: Database(MongoClient(host=['172.169.11.51:27017'], document_class=dict, tz_aware=False, connect=True), 'Aicoin_db')

db = client['Aicoin_db']
db
Out[3]: Database(MongoClient(host=['172.169.11.51:27017'], document_class=dict, tz_aware=False, connect=True), 'Aicoin_db')

3、連線表collection

my_set = db.huobiprohtusdt  #使用test_set集合,沒有則自動建立
db.huobiprohtusdt
Out[4]: Collection(Database(MongoClient(host=['172.169.11.51:27017'], document_class=dict, tz_aware=False, connect=True), 'Aicoin_db'), 'huobiprohtusdt')

#或者
db['huobiprohtusdt']
Collection(Database(MongoClient(host=['172.169.11.51:27017'], document_class=dict, tz_aware=False, connect=True), 'Aicoin_db'), 'huobiprohtusdt')

4、insert 插入操作

使用JSON樣式的文件表示(並存儲)MongoDB中的資料。在PyMongo中,使用字典來表示文件。

import datetime
post = {"author": "bruce",
	          "text": "My first blog post!",
              "tags": ["mongodb", "python", "pymongo"],
	          "date": datetime.datetime.utcnow()}
db = client['test']
myset = db.smart
myset.insert(post)

插入文件時"_id",如果文件尚未包含"_id"金鑰,則會自動新增特殊鍵。"_id"整個集合中的值必須是唯一的。insert_one()返回一個例項InsertOneResult。通過 insert 鍵入post插入:

list(myset.find({'author':'bruce'}))
Out[5]: 
[{'_id': ObjectId('5bb0b0829c90e04d4c6a732d'),
  'author': 'bruce',
  'date': datetime.datetime(2018, 9, 30, 11, 15, 48, 208000),
  'tags': ['mongodb', 'python', 'pymongo'],
  'text': 'My first blog post!'}]

插入post之後,完成了新建smart集合表、和smart表裡post內容的建立。

  • insert插入一個列表多條資料不用遍歷,效率高, save需要遍歷列表,一個個插入
myset.insert([{'author':'smog','bbq':'yes'},{'author':'mosk','bear':'no'}])
Out[221]: [ObjectId('5bb0b34b9c90e04d4c6a732e'), ObjectId('5bb0b34b9c90e04d4c6a732f')]

list(myset.find({}))
Out[222]: 
[{'_id': ObjectId('5bb0b0829c90e04d4c6a732d'),
  'author': 'bruce',
  'date': datetime.datetime(2018, 9, 30, 11, 15, 48, 208000),
  'tags': ['mongodb', 'python', 'pymongo'],
  'text': 'My first blog post!'},
 {'_id': ObjectId('5bb0b34b9c90e04d4c6a732e'), 'author': 'smog', 'bbq': 'yes'},
 {'_id': ObjectId('5bb0b34b9c90e04d4c6a732f'), 'author': 'mosk', 'bear': 'no'}]

如果是save,只能一個個的插入:

myset.save([{'author':'smog','bbq':'yes'},{'author':'mosk','bear':'no'}])
Traceback (most recent call last):

  File "<ipython-input-223-dcaef1972c1f>", line 1, in <module>
    myset.save([{'author':'smog','bbq':'yes'},{'author':'mosk','bear':'no'}])

  File "C:\Users\winnie\Anaconda3\lib\site-packages\pymongo\collection.py", line 3128, in save
    common.validate_is_document_type("to_save", to_save)

  File "C:\Users\winnie\Anaconda3\lib\site-packages\pymongo\common.py", line 453, in validate_is_document_type
    "collections.MutableMapping" % (option,))

TypeError: to_save must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
#單個可插入
myset.save({'author':'kkik','fance':'yes'})
Out[224]: ObjectId('5bb0b3de9c90e04d4c6a7330')

5、find 查詢資料

#查詢全部
for i in myset.find():
    print(i)
#查詢"author = bruce"的
for i in myset.find({"author":"bruce"}):
    print(i)
print(myset.find_one({"author":"bruce"}))

{'_id': ObjectId('5bb0b0829c90e04d4c6a732d'), 'author': 'bruce', 'text': 'My first blog post!', 'tags': ['mongodb', 'python', 'pymongo'], 'date': datetime.datetime(2018, 9, 30, 11, 15, 48, 208000)}
{'_id': ObjectId('5bb0b34b9c90e04d4c6a732e'), 'author': 'smog', 'bbq': 'yes'}
{'_id': ObjectId('5bb0b34b9c90e04d4c6a732f'), 'author': 'mosk', 'bear': 'no'}
{'_id': ObjectId('5bb0b3de9c90e04d4c6a7330'), 'author': 'kkik', 'fance': 'yes'}

{'_id': ObjectId('5bb0b0829c90e04d4c6a732d'), 'author': 'bruce', 'text': 'My first blog post!', 'tags': ['mongodb', 'python', 'pymongo'], 'date': datetime.datetime(2018, 9, 30, 11, 15, 48, 208000)}

{'_id': ObjectId('5bb0b0829c90e04d4c6a732d'), 'author': 'bruce', 'text': 'My first blog post!', 'tags': ['mongodb', 'python', 'pymongo'], 'date': datetime.datetime(2018, 9, 30, 11, 15, 48, 208000)}

6、更新資料

  • 函式表示式
myset.update(
   <query>,    #查詢條件
   <update>,    #update的物件和一些更新的操作符
   {
     upsert: <boolean>,     # mongodb 預設是false,如果不存在update的記錄,是否插入
     multi: <boolean>,        #可選,mongodb 預設是false,只更新找到的第一條記錄
     writeConcern: <document>    #可選,丟擲異常的級別。
   }
)
  • 例項:
myset.update({"author":"bruce"},{'$set':{"text":'i love py'}})
Out[226]: {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}

myset.find_one({"author":"bruce"})
Out[227]: 
{'_id': ObjectId('5bb0b0829c90e04d4c6a732d'),
 'author': 'bruce',
 'date': datetime.datetime(2018, 9, 30, 11, 15, 48, 208000),
 'tags': ['mongodb', 'python', 'pymongo'],
 'text': 'i love py'}

7、刪除資料

my_set.remove(
   <query>,                                   #(可選)刪除的文件的條件
   {
     justOne: <boolean>,                #(可選)如果設為 true 或 1,則只刪除一個文件
     writeConcern: <document>    #(可選)丟擲異常的級別
   }
)
  • 例項
#刪除'author': 'bruce'的全部記錄
myset.remove({'author': 'bruce'})

#刪除'author':'kkik的某個id的記錄
id = myset.find_one({'author':'kkik'})["_id"]
myset.remove(id)

Out[6]: {'n': 1, 'ok': 1.0}

myset.find_one({'author':'bruce'})
#返回NaN
myset.find_one({'author':'kkik'})
#返回NaN

#刪除集合裡的所有記錄
db.smart.remove()
Out[7]: {'n': 2, 'ok': 1.0}

list(myset.find())
Out[8]: []

8、mongodb的條件操作符

#    (>)  大於 - $gt
#    (<)  小於 - $lt
#    (>=)  大於等於 - $gte
#    (<= )  小於等於 - $lte
  • 例項:
db.smart.insert([{'BB':5,'book':'Y'},{'BB':5,'juice':'Y'},{'BB':5,'shit':'Y'},{'BB':5,'lover':'N'}])
Out[8]: 
[ObjectId('5bb0bc3e9c90e04d4c6a7331'),
 ObjectId('5bb0bc3e9c90e04d4c6a7332'),
 ObjectId('5bb0bc3e9c90e04d4c6a7333'),
 ObjectId('5bb0bc3e9c90e04d4c6a7334')]
 
list(db.smart.find())
Out[9]: 
[{'BB': 5, '_id': ObjectId('5bb0bc3e9c90e04d4c6a7331'), 'book': 'Y'},
 {'BB': 5, '_id': ObjectId('5bb0bc3e9c90e04d4c6a7332'), 'juice': 'Y'},
 {'BB': 5, '_id': ObjectId('5bb0bc3e9c90e04d4c6a7333'), 'shit': 'Y'},
 {'BB': 5, '_id': ObjectId('5bb0bc3e9c90e04d4c6a7334'), 'lover': 'N'}]

for i in list(db.smart.find({"BB":{"$gt":1}})):
    print(i)
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7331'), 'BB': 5, 'book': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7332'), 'BB': 5, 'juice': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7333'), 'BB': 5, 'shit': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7334'), 'BB': 5, 'lover': 'N'}

9、type(判斷型別)

Double    1     
String    2     
Object    3     
Array    4     
Binary data    5     
Undefined    6    已廢棄
Object id    7     
Boolean    8     
Date    9     
Null    10     
Regular Expression    11     
JavaScript    13     
Symbol    14     
JavaScript (with scope)    15     
32-bit integer    16     
Timestamp    17     
64-bit integer    18     
Min key    255    Query with -1.
Max key    127     
  • 例項
for i in list(db.smart.find({"BB":{"$type":16}})):      #32-bit intege 32位整型
    print(i)
    
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7331'), 'BB': 5, 'book': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7332'), 'BB': 5, 'juice': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7333'), 'BB': 5, 'shit': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7334'), 'BB': 5, 'lover': 'N'}

10、SORT-POP-PILL-IN-OR-ALL-LIMIT-SKIP

  • 在MongoDB中使用sort()方法對資料進行排序,sort()方法可以通過引數指定排序的欄位,並使用 1 和 -1 來指定排序的方式,其中 1 為升序,-1為降序。
for i in db.smart.find().sort([("BB",1)]):
    print(i)
    
{'_id': ObjectId('5bb0be259c90e04d4c6a7335'), 'BB': 1, 'biik': 'Y'}
{'_id': ObjectId('5bb0be259c90e04d4c6a7336'), 'BB': 2, 'jddce': 'Y'}
{'_id': ObjectId('5bb0be259c90e04d4c6a7337'), 'BB': 3, 'shiiit': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7331'), 'BB': 5, 'book': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7332'), 'BB': 5, 'juice': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7333'), 'BB': 5, 'shit': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7334'), 'BB': 5, 'lover': 'N'}
{'_id': ObjectId('5bb0be259c90e04d4c6a7338'), 'BB': 7, 'loveffr': 'N'}
  • limit和skip
#limit()方法用來讀取指定數量的資料
#skip()方法用來跳過指定數量的資料

for i in db.smart.find().skip(2).limit(6):
    print(i)
    
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7333'), 'BB': 5, 'shit': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7334'), 'BB': 5, 'lover': 'N'}
{'_id': ObjectId('5bb0be259c90e04d4c6a7335'), 'BB': 1, 'biik': 'Y'}
{'_id': ObjectId('5bb0be259c90e04d4c6a7336'), 'BB': 2, 'jddce': 'Y'}
{'_id': ObjectId('5bb0be259c90e04d4c6a7337'), 'BB': 3, 'shiiit': 'Y'}
{'_id': ObjectId('5bb0be259c90e04d4c6a7338'), 'BB': 7, 'loveffr': 'N'}
  • IN
#找出BB是1、3的資料

for i in db.smart.find({"BB":{"$in":(1,3)}}):
    print(i)
    
{'_id': ObjectId('5bb0be259c90e04d4c6a7335'), 'BB': 1, 'biik': 'Y'}
{'_id': ObjectId('5bb0be259c90e04d4c6a7337'), 'BB': 3, 'shiiit': 'Y'}
  • OR

#找出BB是2或BB是5的記錄

for i in db.smart.find({"$or":[{"BB":2},{"BB":5}]}):
    print(i)
    
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7331'), 'BB': 5, 'book': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7332'), 'BB': 5, 'juice': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7333'), 'BB': 5, 'shit': 'Y'}
{'_id': ObjectId('5bb0bc3e9c90e04d4c6a7334'), 'BB': 5, 'lover': 'N'}
{'_id': ObjectId('5bb0be259c90e04d4c6a7336'), 'BB': 2, 'jddce': 'Y'}
  • all
aa = {"bb":"sad","age":19,"fg":[2,3]}
vv = {"bb":"joke","age":29,"fg":[1,2,3,4,5,6]}


db.smart.insert([aa,vv])
Out[256]: [ObjectId('5bb0bfcf9c90e04d4c6a7339'), ObjectId('5bb0bfcf9c90e04d4c6a733a')]

for i in db.smart.find({'fg':{'$all':[1,2,3,4]}}):
    print(i)
    
{'_id': ObjectId('5bb0bfcf9c90e04d4c6a733a'), 'bb': 'joke', 'age': 29, 'fg': [1, 2, 3, 4, 5, 6]}
  • pop/pull/pullAll

1、pop移除最後一個元素

#pop
#移除最後一個元素(-1為移除第一個)
db.smart.update({'bb':'joke'}, {'$pop':{'fg':1}})
for i in db.smart.find({'bb':'joke'}):
    print(i)
    
{'_id': ObjectId('5bb0bfcf9c90e04d4c6a733a'), 'bb': 'joke', 'age': 29, 'fg': [1, 2, 3, 4, 5]}

2、pull 按值移除

db.smart.update({'bb':'joke'}, {'$pull':{'fg':3}})
Out[263]: {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}

3、pullAll移除全部符合條件的

db.smart.update({'bb':'joke'}, {'$pullAll':{'fg':[1,2,4]}})
Out[266]: {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}

11、多級路徑元素操作

#多級目錄用. 連線查詢
dic = {"name":"bruce",
       "age":20,
       "contact" : {
           "email" : "[email protected]",
           "iphone" : "10101010"}
       }
db.smart.insert(dic)
       
Out[267]: ObjectId('5bb0c5ba9c90e04d4c6a733b')

for i in db.smart.find({"contact.iphone":"10101010"}):
    print(i)
    
{'_id': ObjectId('5bb0c5ba9c90e04d4c6a733b'), 'name': 'bruce', 'age': 20, 'contact': {'email': '[email protected]', 'iphone': '10101010'}}

result = db.smart.find_one({"contact.iphone":"10101010"})
print(result["contact"]["email"])

[email protected]

#多級路徑下修改操作
result = db.smart.update({"contact.iphone":"10101010"},{"$set":{"contact.email":"[email protected]"}})
result1 = db.smart.find_one({"contact.iphone":"10101010"})
print(result1["contact"]["email"])

[email protected]
  • 還可以對陣列用索引操作
dic = {"name":"lii",
       "age":18,
       "contact" : [
           {
           "email" : "[email protected]",
           "iphone" : "111"},
           {
           "email" : "[email protected]",
           "iphone" : "222"}
       ]}
db.smart.insert(dic)
       
Out[295]: ObjectId('5bb0c8789c90e04d4c6a733f')

result1 = db.smart.find_one({"contact.1.iphone":"222"})
print(result1)

{'_id': ObjectId('5bb0c6979c90e04d4c6a733c'), 'name': 'bruceLi', 'age': 21, 'contact': [{'email': '[email protected]', 'iphone': '111'}, {'email': '[email protected]', 'iphone': '222'}]}

result = db.smart.update({"contact.1.iphone":"222"},{"$set":{"contact.1.email":"[email protected]"}})
print(result1["contact"][1]["email"])

[email protected]

12、完整指令碼

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from pymongo import MongoClient

settings = {
    "ip":'localhost',                #ip
    "port":27017,                 #埠
    "db_name" : "mydb",    #資料庫名字
    "set_name" : "test_set"   #集合名字
}

class MyMongoDB(object):
    def __init__(self):
        try:
            self.conn = MongoClient(settings["ip"], settings["port"])
        except Exception as e:
            print(e)
        self.db = self.conn[settings["db_name"]]
        self.my_set = self.db[settings["set_name"]]

    def insert(self,dic):
        print("inser...")
        self.my_set.insert(dic)

    def update(self,dic,newdic):
        print("update...")
        self.my_set.update(dic,newdic)

    def delete(self,dic):
        print("delete...")
        self.my_set.remove(dic)

    def dbfind(self,dic):
        print("find...")
        data = self.my_set.find(dic)
        for result in data:
            print(result["name"],result["age"])

def main():
    dic={"name":"bruce","age":20}
    mongo = MyMongoDB()
    mongo.insert(dic)
    mongo.dbfind({"name":"bruce"})

    mongo.update({"name":"bruce"},{"$set":{"age":"25"}})
    mongo.dbfind({"name":"bruce"})

    mongo.delete({"name":"bruce"})
    mongo.dbfind({"name":"bruce"})

if __name__ == "__main__":
    main()

inser...
find...
bruce 20
update...
find...
bruce 25
delete...
find...