【轉】Python操做MongoDB數據庫

  • 前言
  • MongoDB GUI 工具
  • PyMongo(同步)
  • Motor(異步)
  • 後記

前言

最近這幾天準備介紹一下 Python 與三大數據庫的使用,這是第一篇,首先來介紹 MongoDB 吧,,走起!!python

MongoDB GUI 工具

首先介紹一款 MongoDB 的 GUI 工具 Robo 3T,初學 MongoDB 用這個來查看數據真的很爽。能夠即時看到數據的增刪改查,不用操做命令行來查看。 git

操做界面圖

PyMongo(同步)

可能你們都對 PyMongo 比較熟悉了,這裏就簡單介紹它的增刪改查等操做。程序員

鏈接

# 普通鏈接
client = MongoClient('localhost', 27017)
client = MongoClient('mongodb://localhost:27017/')
#
# 密碼鏈接
client = MongoClient('mongodb://username:password@localhost:27017/dbname')
db = client.zfdb
# db = client['zfdb']

test = db.test
View Code

# 增長一條記錄
person = {'name': 'zone','sex':'boy'}
person_id = test.insert_one(person).inserted_id
print(person_id)
# 批量插入
persons = [{'name': 'zone', 'sex': 'boy'}, {'name': 'zone1', 'sex': 'boy1'}]
result = test.insert_many(persons)
print(result.inserted_ids)
View Code

# 刪除單條記錄
result1 = test.delete_one({'name': 'zone'})
pprint.pprint(result1)
# 批量刪除
result1 = test.delete_many({'name': 'zone'})
pprint.pprint(result1)
View Code

# 更新單條記錄
res = test.update_one({'name': 'zone'}, {'$set': {'sex': 'girl girl'}})
print(res.matched_count)
# 更新多條記錄
test.update_many({'name': 'zone'}, {'$set': {'sex': 'girl girl'}})
View Code

# 查找多條記錄
pprint.pprint(test.find())

# 添加查找條件
pprint.pprint(test.find({"sex": "boy"}).sort("name"))
View Code

聚合

若是你是個人老讀者,那麼你確定知道我以前的騷操做,就是用爬蟲爬去數據以後,用聚合統計結合可視化圖表進行數據展現。github

aggs = [
    {"$match": {"$or" : [{"field1": {"$regex": "regex_str"}}, {"field2": {"$regex": "regex_str"}}]}}, # 正則匹配字段
    {"$project": {"field3":1, "field4":1}},# 篩選字段 
    {"$group": {"_id": {"field3": "$field3", "field4":"$field4"}, "count": {"$sum": 1}}}, # 聚合操做
]

result = test.aggregate(pipeline=aggs)
View Code

例子:以分組的方式統計 sex 這個關鍵詞出現的次數,說白了就是統計有多少個男性,多少個女性。mongodb

test.aggregate([{'$group': {'_id': '$sex', 'weight': {'$sum': 1}}}])
View Code

聚合效果圖:(秋招季,用Python分析深圳程序員工資有多高?文章配圖) Python 工做年限要求 Python 學歷要求數據庫

Motor(異步)

Motor 是一個異步實現的 MongoDB 存儲庫 Motor 與 Pymongo 的配置基本相似。鏈接對象就由 MongoClient 變爲 AsyncIOMotorClient 了。下面進行詳細介紹一下。異步

鏈接

# 普通鏈接
client = motor.motor_asyncio.AsyncIOMotorClient('mongodb://localhost:27017')
# 副本集鏈接
client = motor.motor_asyncio.AsyncIOMotorClient('mongodb://host1,host2/?replicaSet=my-replicaset-name')
# 密碼鏈接
client = motor.motor_asyncio.AsyncIOMotorClient('mongodb://username:password@localhost:27017/dbname')
# 獲取數據庫
db = client.zfdb
# db = client['zfdb']
# 獲取 collection
collection = db.test
# collection = db['test']
View Code

增長一條記錄

添加一條記錄。async

async def do_insert():
     document = {'name': 'zone','sex':'boy'}
     result = await db.test_collection.insert_one(document)
     print('result %s' % repr(result.inserted_id))
loop = asyncio.get_event_loop()
loop.run_until_complete(do_insert())
View Code

增長一條記錄

批量增長記錄

添加結果如圖所暗示。ide

async def do_insert():
    result = await db.test_collection.insert_many(
        [{'name': i, 'sex': str(i + 2)} for i in range(20)])
    print('inserted %d docs' % (len(result.inserted_ids),))

loop = asyncio.get_event_loop()
loop.run_until_complete(do_insert())
View Code

批量增長記錄

查找一條記錄

async def do_find_one():
    document = await db.test_collection.find_one({'name': 'zone'})
    pprint.pprint(document)

loop = asyncio.get_event_loop()
loop.run_until_complete(do_find_one())
View Code

查找一條記錄

查找多條記錄

查找記錄能夠添加篩選條件。工具

async def do_find():
    cursor = db.test_collection.find({'name': {'$lt': 5}}).sort('i')
    for document in await cursor.to_list(length=100):
        pprint.pprint(document)

loop = asyncio.get_event_loop()
loop.run_until_complete(do_find())

# 添加篩選條件,排序、跳過、限制返回結果數
async def do_find():
    cursor = db.test_collection.find({'name': {'$lt': 4}})
    # Modify the query before iterating
    cursor.sort('name', -1).skip(1).limit(2)
    async for document in cursor:
        pprint.pprint(document)

loop = asyncio.get_event_loop()
loop.run_until_complete(do_find())
View Code

查找多條記錄

統計

async def do_count():
    n = await db.test_collection.count_documents({})
    print('%s documents in collection' % n)
    n = await db.test_collection.count_documents({'name': {'$gt': 1000}})
    print('%s documents where i > 1000' % n)

loop = asyncio.get_event_loop()
loop.run_until_complete(do_count())
View Code

統計

替換

替換則是將除 id 之外的其餘內容所有替換掉。

async def do_replace():
    coll = db.test_collection
    old_document = await coll.find_one({'name': 'zone'})
    print('found document: %s' % pprint.pformat(old_document))
    _id = old_document['_id']
    result = await coll.replace_one({'_id': _id}, {'sex': 'hanson boy'})
    print('replaced %s document' % result.modified_count)
    new_document = await coll.find_one({'_id': _id})
    print('document is now %s' % pprint.pformat(new_document))

loop = asyncio.get_event_loop()
loop.run_until_complete(do_replace())
View Code

替換

更新

更新指定字段,不會影響到其餘內容。

async def do_update():
    coll = db.test_collection
    result = await coll.update_one({'name': 0}, {'$set': {'sex': 'girl'}})
    print('更新條數: %s ' % result.modified_count)
    new_document = await coll.find_one({'name': 0})
    print('更新結果爲: %s' % pprint.pformat(new_document))

loop = asyncio.get_event_loop()
loop.run_until_complete(do_update())
View Code

更新

刪除

刪除指定記錄。

async def do_delete_many():
    coll = db.test_collection
    n = await coll.count_documents({})
    print('刪除前有 %s 條數據' % n)
    result = await db.test_collection.delete_many({'name': {'$gte': 10}})
    print('刪除後 %s ' % (await coll.count_documents({})))

loop = asyncio.get_event_loop()
loop.run_until_complete(do_delete_many())
View Code

刪除

後記

MongoDB 的騷操做就介紹到這裏,後面會繼續寫 MySQL 和 Redis 的騷操做。盡請期待。

zone7

相關文章
相關標籤/搜索