电竞比分网-中国电竞赛事及体育赛事平台

分享

python必掌握庫(kù):pymongo庫(kù)的心你懂嗎?

 liqualife 2019-09-18

前言:

工欲善其事必先利其器,用pymongo庫(kù)之前,大家需首先對(duì)MongoDB數(shù)據(jù)庫(kù)的增刪改查操作有一些基礎(chǔ)方法的了解。

我們?cè)谶@里使用linux上的ipython交互環(huán)境來(lái)pymongo庫(kù)的知識(shí)點(diǎn)系統(tǒng)性講解。

一:如何用python連接MongoDB數(shù)據(jù)庫(kù)?

1.1、使用MongoClient()方法建立連接

import pandas as pd
from pymongo import MongoClient
#實(shí)例化client,建立連接
client = MongoClient(host='localhost', port=27017)
collection = MongoClient('',27017)['predict_monogo']['t_third_data']

host:指定地址 localhost:XXX.XX.X.XXX

port:指定端口 27017

把創(chuàng)立好的MongoDB的連接對(duì)象賦值為client

1.2、使用URL建立連接

另外MongoClient的第一個(gè)參數(shù)host還可以直接傳MongoDB的連接字符串,以mongodb開(kāi)頭,例如:

client = MongoClient('mongodb://localhost:27017/')

1.3 指定數(shù)據(jù)庫(kù)

MongoDB中有許許多多個(gè)生產(chǎn)庫(kù),我們需要指定具體操作的數(shù)據(jù)庫(kù)。

我們調(diào)用client的test屬性即可返回test數(shù)據(jù)庫(kù)

db = client.test

或者

db = client['test']

1.4 指定集合

MongoDB的每個(gè)數(shù)據(jù)庫(kù)又包含了許多集合Collection,也就類似與關(guān)系型數(shù)據(jù)庫(kù)中的表,用show tables命令可以查看數(shù)據(jù)庫(kù)中的各個(gè)集合名稱。下一步我們需要指定要操作的集合,在這里我們指定一個(gè)集合名稱為mygirlfriend(女朋友集合),指定集合也有兩種方式。

指定集合方式1

collection = db.mygirlfriend

指定集合方式2

collection = db['mygirlfriend']

二、如何插入數(shù)據(jù)?

2.1、insert_one()方法插入單條數(shù)據(jù)

接著,我們來(lái)插入數(shù)據(jù)myboyfriend

#對(duì)于myboyfriend這個(gè)Collection,我們新建一條男朋友數(shù)據(jù),以字典的形式表示:
myboyfriend = {
'id': '001',
'name': 'shuhao',
'age': 24,
'hometown': 'SHENZHEN'
}
result2 = collection.insert_one(myboyfriend)

輸出插入的文檔對(duì)應(yīng)的 _id 值

insert_one()方法返回InsertOneResult對(duì)象,該對(duì)象包含inserted_id屬性,它是插入文檔的id值。(在MongoDB中,每條數(shù)據(jù)其實(shí)都有一個(gè)_id屬性來(lái)唯一標(biāo)識(shí),如果沒(méi)有顯式指明_id,MongoDB會(huì)自動(dòng)產(chǎn)生一個(gè)ObjectId類型的_id屬性

2.2、insert_many()方法插入多條數(shù)據(jù)

#我們可以將數(shù)據(jù)以列表形式傳遞即可
mygirlfriend = [
{ 'id': '001', 'name': 'ROSE', 'age': '18', 'hometown': 'beijing' },
{ 'id': '002', 'name': 'ANGELA', 'age': '22', 'hometown': 'SHENZHEN' },
{ 'id': '003', 'name': 'JOSIE', 'age': '24' , 'hometown': 'SHENZHEN'},
{ 'id': '004', 'name': 'CINDY', 'age': '26', 'hometown': 'SHENZHEN' },
{ 'id': '005', 'name': 'SUNNT', 'age': '36' , 'hometown': 'NEWYORK'}
]
result1 = collection.insert_many(mygirlfriend)

輸出插入的所有文檔對(duì)應(yīng)的 _id 值

print(result1.inserted_ids

insert_many()方法返回InsertManyResult對(duì)象,該對(duì)象包含inserted_ids屬性,該屬性保存著所有插入文檔的id值。

執(zhí)行完以上查找,我們可以在命令終端,查看mygirlfriend 數(shù)據(jù)是否已插入:

db.mygirlfriend.find()

三、如何查詢數(shù)據(jù)?

3.1find_one()方法來(lái)查詢mygirlfriend集合中的一條數(shù)據(jù)。

client = MongoClient('XXX.XX.X.XXX',27017)
db=client.test
collection = db.mygirlfriend
x = collection.find_one()
print(x)

接著,我們?cè)賮?lái)根據(jù)指定條件查詢細(xì)分查詢

在這里我們查詢name為JOSIE的mygirlfriend集合數(shù)據(jù),它的返回結(jié)果是字典類型,運(yùn)行結(jié)果:

result = collection.find_one({'name': 'JOSIE'})
print(type(result))
print(result)

_id屬性 ObjectId('5c67c624332d6344f9ce55e4')是MongoDB在插入的過(guò)程中自動(dòng)添加的。

我們也可以直接根據(jù)ObjectId來(lái)查詢name為JOSIE的mygirlfriend集合數(shù)據(jù),這里需要使用bson庫(kù)里面的ObjectId。

from bson.objectid import ObjectId
result = collection.find_one({'_id': ObjectId('5c67c624332d6344f9ce55e4')})
print(result)

3.1find()方法來(lái)查詢mygirlfriend集合中的多條數(shù)據(jù)。

Ex:在這里查詢mygirlfriend集合中的查找hometown為SHENZHEN的數(shù)據(jù)

results = collection.find({'hometown': 'SHENZHEN'})
print(results)

其返回結(jié)果是個(gè)Cursor類型?。。。ㄏ喈?dāng)于一個(gè)生成器)

我們需要把所有的結(jié)果遍歷取出即可!??!

for result in results:
print(result)

如果要查詢查詢mygirlfriend集合中年齡大于24的數(shù)據(jù)要怎么寫呢?

#如果要查詢年齡大于24的數(shù)據(jù),則寫法如下:
result1 = collection.find({'age': {'$gt': 24}})
print(result1)

在這里查詢的條件鍵值已經(jīng)不是單純的數(shù)字了,而是一個(gè)字典,其鍵名為比較符號(hào)$gt,意思是大于,鍵值為24,這樣便可以查詢出所有年齡大于24的數(shù)據(jù)。

在這里將比較符號(hào)歸納如下表:

符號(hào)含義示例
$lt小于{'age': {'$lt': 20}}
$gt大于{'age': {'$gt': 20}}
$lte小于等于{'age': {'$lte': 20}}
$gte大于等于{'age': {'$gte': 20}}
$ne不等于{'age': {'$ne': 20}}
$in在范圍內(nèi){'age': {'$in': [20, 23]}}
$nin不在范圍內(nèi){'age': {'$nin': [20, 23]}}

在這里,我們可以把數(shù)據(jù)轉(zhuǎn)換成pandas的DataFrame的數(shù)據(jù)格式打印出來(lái)。

result2 = pd.DataFrame(list(result1))
print(result2)

四、count()方法計(jì)數(shù)和sort() 方法排序

4.1 要統(tǒng)計(jì)查詢結(jié)果有多少條數(shù)據(jù),可以調(diào)用count()方法

如統(tǒng)計(jì)所有數(shù)據(jù)條數(shù):

count = collection.find().count()
print(count)

查詢hometown為NEWYORK的數(shù)據(jù)條數(shù)!

count = collection.find({'hometown': 'NEWYORK'}).count()
print(count)

4.2、sort() 方法排序

sort() 方法第一個(gè)參數(shù)為要排序的字段,第二個(gè)字段指定排序規(guī)則,1 為升序,-1 為降序,默認(rèn)為升序。

#排序
mydoc = collection.find().sort('name',1)
for x in mydoc:
print(x)

五、偏移和更新操作

5.1、偏移

在某些情況下我們可能想取某幾個(gè)元素可以利用skip()方法偏移幾個(gè)位置。

例如偏移2,就忽略前2個(gè)元素,得到第三個(gè)及以后的元素。

results = collection.find().sort('name').skip(2)
print([result['name'] for result in results])

5.2、更新操作

5.2.1、 update_one() 方法修改文檔中的記錄

第一個(gè)參數(shù)為查詢的條件

第二個(gè)參數(shù)為要修改的字段,需要使用newvalues = { '$set': { 'name': 'baby' } }這樣的形式

如果查找到的匹配數(shù)據(jù)多余一條,則只會(huì)修改第一條。

myquery = { 'name': 'SUNNT' }
newvalues = { '$set': { 'name': 'baby' } }
collection.update_one(myquery, newvalues)

輸出修改后的集合

for x in collection.find():
print(x)

5.2.2、update_many() 方法修改文檔中的記錄

更改hometown為以 S 開(kāi)頭的文檔數(shù)據(jù)在mygirlfriend集合為hometown為HONGKONG

 myquery = { 'hometown': { '$regex': '^S' } }
newvalues = { '$set': { 'hometown': 'HONGKONG' } }

x = collection.update_many(myquery, newvalues)

查看數(shù)據(jù)

更新好了之后,我們?cè)賮?lái)隨意查一條數(shù)據(jù)

#花式查詢
mydoc=collection.find({'hometown':'HONGKONG','name':'ANGELA'})

for x in mydoc:
print(x)

六、如何用pymongo 刪除數(shù)據(jù)?

6.1delete_many()方法刪除多條文檔

刪除所有 hometown字段中以 S 開(kāi)頭的文檔:

delete_many()方法第一個(gè)參數(shù)為查詢對(duì)象,指定要?jiǎng)h除哪些數(shù)據(jù)

myquery = { 'hometown': {'$regex': '^S'} }
x = collection.delete_many(myquery)
print(x.deleted_count, '個(gè)文檔已刪除')

以 S 開(kāi)頭的文檔數(shù)據(jù)在mygirlfriend集合已經(jīng)刪除了

在這里使用了$regex來(lái)指定正則匹配,^S.*代表以S開(kāi)頭的正則表達(dá)式,這樣就可以查詢所有符合該正則的結(jié)果。

在這里將一些功能符號(hào)再歸類如下:

符號(hào)含義示例示例含義
$regex匹配正則{'name': {'$regex': '^M.*'}}name以M開(kāi)頭
$exists屬性是否存在{'name': {'$exists': True}}name屬性存在
$type類型判斷{'age': {'$type': 'int'}}age的類型為int
$mod數(shù)字模操作{'age': {'$mod': [5, 0]}}年齡模5余0
$text文本查詢{'$text': {'$search': 'Mike'}}text類型的屬性中包含Mike字符串
$where高級(jí)條件查詢{'$where': 'obj.fans_count == obj.follows_count'}自身粉絲數(shù)等于關(guān)注數(shù)
這些操作的更詳細(xì)用法在可以在MongoDB官方文檔找到:
docs.mongodb.com/manual

6.2delete_one()方法刪除單個(gè)文檔

以下實(shí)例刪除 name 字段值為 'ROSE' 的文檔:

myquery = { 'name': 'ROSE' }
collection.delete_one(myquery)
# 刪除后輸出
for x in collection.find():
print(x)

那么如何刪除集合中的所有文檔呢?

x = collection.delete_many({})
#deleted_count屬性獲取刪除的數(shù)據(jù)條數(shù)
print(x.deleted_count, '個(gè)文檔已刪除')

delete_many()方法如果傳入的是一個(gè)空的查詢對(duì)象,則會(huì)刪除集合中的所有文檔:

deleted_count屬性獲取刪除的數(shù)據(jù)條數(shù)

調(diào)用db.mygirlfriend.find()方法可以看到數(shù)據(jù)最后一條數(shù)據(jù)也被我們刪除了,mygirlfriend集合的數(shù)據(jù)已經(jīng)被我們刪光了。

但是這個(gè)時(shí)候這個(gè)集合還是在的

接著,我們調(diào)用collection.drop()方法即可刪除集合。

上圖可以看到,mygirlfriend集合已經(jīng)被刪除啦!

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多