欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 操作 MongoDB 講解詳細(xì)

 更新時(shí)間:2021年09月27日 14:23:35   作者:tigeriaf  
MongoDB是一款開源的、基于分布式的、面向文檔存儲(chǔ)的非關(guān)系型數(shù)據(jù)庫(kù)。擁有高性能、高可用,易擴(kuò)展的優(yōu)點(diǎn),并且支持豐富的查詢語言來支持讀寫操作以及更復(fù)雜的查詢等。接下來我們來看一下如何使用Python操作MongoDB數(shù)據(jù)庫(kù)

1、連接MongoDB

需要使用Python第三方庫(kù)pymongo來連接以及操作MongoDB,可以使用pip install pymongo進(jìn)行安裝。 可以使用下面代碼來創(chuàng)建一個(gè)MongoDB的連接對(duì)象。

import pymongo


client = pymongo.MongoClient(host='localhost', port=27017)

一般來說傳入兩個(gè)參數(shù)就可以,第一個(gè)參數(shù)為地址host(默認(rèn)是localhost)第二個(gè)參數(shù)為端口port(默認(rèn)是27017)。
還有一種方法是host參數(shù)直接傳入MongoDB的連接字符串,例如:

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

2、指定(切換)數(shù)據(jù)庫(kù)

db = client.test
# 或者
# db = client['test']

如果該數(shù)據(jù)庫(kù)不存在,則自動(dòng)創(chuàng)建,否則切換到指定數(shù)據(jù)庫(kù)。 注意: 新創(chuàng)建的數(shù)據(jù)庫(kù),在沒有插入數(shù)據(jù)之前在可視化工具里看不到。

3、指定(切換)集合

MongoDB的數(shù)據(jù)庫(kù)中包含很多集合collection,類似于關(guān)系型數(shù)據(jù)庫(kù)中的表,同樣,我們可以使用下面和指定數(shù)據(jù)庫(kù)類似的方式,指定要操作的集合。

collection = db.users
# 或者
collection = db['users']

4、插入數(shù)據(jù)

調(diào)用collection的insert_one()方法可以插入單條數(shù)據(jù)。

user = {
    'name': 'tigeriaf',
    'gender': 'male',
    'age': 24
}

collection.insert_one(user)

MongoDB中,每條數(shù)據(jù)都有一個(gè)唯一的_id屬性,如果沒有顯式的指明_id,MongoDB會(huì)自動(dòng)生成ObjectId類型的_id屬性。

當(dāng)然我們也可以插入多條數(shù)據(jù),使用的是insert_many()方法,數(shù)據(jù)以列表形式傳遞。

user_list = [
    {
        'name': 'zhangsan',
        'gender': 'male',
        'age': 25
    },
    {
        'name': 'lisi',
        'gender': 'male',
        'age': 24

    },
    {
        'name': 'wangwu',
        'gender': 'female',
        'age': 24

    }
]

collection.insert_many(user_list)

5、查詢數(shù)據(jù)

通過find_one()find()方法進(jìn)行數(shù)據(jù)的查詢,find_one()查詢返回單個(gè)結(jié)果,find()返回多個(gè)結(jié)果。

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

我們查詢的是nametigeriaf的數(shù)據(jù),返回結(jié)果是字典類型,運(yùn)行結(jié)果如下:

<class 'dict'> {'_id': ObjectId('614be85f1cc0a98d6f034de7'), 'name': 'tigeriaf', 'gender': 'male'}

對(duì)于多條數(shù)據(jù)的查詢,我們可以使用find()方法,例如在這里查找年齡為20的數(shù)據(jù),示例如下:

results = collection.find({'gender': "male"})
print(results)
for result in results:
    print(result)

運(yùn)行結(jié)果如下:

<pymongo.cursor.Cursor object at 0x0BDF8210>
{'_id': ObjectId('614be85f1cc0a98d6f034de7'), 'name': 'tigeriaf', 'gender': 'male'}
{'_id': ObjectId('614beb3ad0f17d253e2ef81c'), 'name': 'zhangsan', 'gender': 'male'}
{'_id': ObjectId('614beb3ad0f17d253e2ef81d'), 'name': 'lisi', 'gender': 'male'}

返回結(jié)果是Cursor類型,我們可以遍歷取到所有的結(jié)果。

6、統(tǒng)計(jì)查詢

可以調(diào)用count()方法來統(tǒng)計(jì)查詢結(jié)果的條數(shù)。

count = collection.find({'gender': "male"}).count()
print(count)

7、結(jié)果排序

可以調(diào)用sort()方法對(duì)查詢的數(shù)據(jù)進(jìn)行排序。

results = collection.find().sort('name', pymongo.ASCENDING)
for result in results:
    print(result)

運(yùn)行結(jié)果如下:

{'_id': ObjectId('614bf7fca5af6d1d46df0878'), 'name': 'lisi', 'gender': 'male', 'age': 24}
{'_id': ObjectId('614bf72ab1b973eae1b32fee'), 'name': 'tigeriaf', 'gender': 'male', 'age': 24}
{'_id': ObjectId('614bf7fca5af6d1d46df0879'), 'name': 'wangwu', 'gender': 'female', 'age': 24}
{'_id': ObjectId('614bf7fca5af6d1d46df0877'), 'name': 'zhangsan', 'gender': 'male', 'age': 25}

8、偏移

在某些情況下我們可能只想獲取某幾條數(shù)據(jù),可以使用skip()方法進(jìn)行偏移操作,比如skip(2) ,就忽略前2條數(shù)據(jù),得到第3條之后的數(shù)據(jù)。

results = collection.find().sort('name', pymongo.ASCENDING).skip(2)
for result in results:
    print(result)

運(yùn)行結(jié)果如下:

{'_id': ObjectId('614bf7fca5af6d1d46df0879'), 'name': 'wangwu', 'gender': 'female', 'age': 24}
{'_id': ObjectId('614bf7fca5af6d1d46df0877'), 'name': 'zhangsan', 'gender': 'male', 'age': 25}

另外還可以使用limit()方法限制結(jié)果個(gè)數(shù)。

results = collection.find().sort('name', pymongo.ASCENDING).skip(1).limit(2)
for result in results:
    print(result)

運(yùn)行結(jié)果如下:

{'_id': ObjectId('614bf72ab1b973eae1b32fee'), 'name': 'tigeriaf', 'gender': 'male', 'age': 24}
{'_id': ObjectId('614bf7fca5af6d1d46df0879'), 'name': 'wangwu', 'gender': 'female', 'age': 24}

9、更新數(shù)據(jù)

可以使用update_one()方法和update_many()方法對(duì)數(shù)據(jù)進(jìn)行更新,update_one()方法更新一條數(shù)據(jù),update_many()方法更新多條數(shù)據(jù)。

condition = {'name': 'wangwu'}
user = collection.find_one(condition)
user['age'] += 1
result = collection.update_one(condition, {'$set': user})
print(result)
print(result.matched_count, result.modified_count)

在這里調(diào)用了update_one()方法修改namewangwu的數(shù)據(jù),第二個(gè)參數(shù)需要使用$類型操作符作為字典的鍵名,返回結(jié)果調(diào)用matched_countmodified_count屬性可以獲得匹配的數(shù)據(jù)條數(shù)和影響的數(shù)據(jù)條數(shù)。

運(yùn)行結(jié)果如下:

<pymongo.results.UpdateResult object at 0x0C96E738>
1 1

調(diào)用update_many()方法會(huì)更新所有符合條件的數(shù)據(jù)。

condition = {'age': 24}
result = collection.update_many(condition, {'$inc': {'age': 1}})
print(result)
print(result.matched_count, result.modified_count)

上述代碼指定了查詢條件為age等于24,然后更新條件為{'$inc': {'age': 1}} ,也就是age加1。

<pymongo.results.UpdateResult object at 0x0CB628A0>
2 2

可以看到更新了多條數(shù)據(jù)。

10、刪除數(shù)據(jù)

可以使用delete_one()方法和delete_many()方法刪除數(shù)據(jù),delete_one()方法刪除一條數(shù)據(jù),delete_many()方法刪除多條數(shù)據(jù)。

result = collection.delete_one({'name': 'zhangsan'})
print(result.deleted_count)
result = collection.delete_many({'gender': "male"})
print(result.deleted_count)

運(yùn)行結(jié)果如下:

1
2

delete_one()刪除的是第一條符合條件的數(shù)據(jù),delete_many()刪除的是所有符合條件的數(shù)據(jù)。

到此這篇關(guān)于Python 操作 MongoDB 講解詳細(xì)的文章就介紹到這了,更多相關(guān)Python 操作 MongoDB內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論