最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺析Python與Mongodb數(shù)據(jù)庫之間的操作方法

 更新時間:2019年07月01日 09:50:32   作者:GYT0313  
這篇文章主要介紹了Python與Mongodb數(shù)據(jù)庫之間的操作,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

MongoDB 是目前最流行的 NoSQL 數(shù)據(jù)庫之一,使用的數(shù)據(jù)類型 BSON(類似 JSON)。

1. 安裝Mongodb和pymongo

Mongodb的安裝和配置

Mongodb的安裝教程請網(wǎng)上搜索, 安裝完成后,    進行以下配置過程:

1.1 創(chuàng)建目錄, 該目錄為Mongodb數(shù)據(jù)文件的存放目錄:

*注: 本人使用的不是root用戶, 所以修改目錄的擁有者. *

sudo mkdir /data
sudo chown -R python:python /data
mkdir /data/db

1.2 分別執(zhí)行命令:

第一條命令為指定端口和保存路徑, 第二條為運行mongodb數(shù)據(jù)庫.

mongod --port 27017 --dbpath /data/db
mongo --port 27017

1.3 安裝pymongo

sudo pip3 install pymongo

2. 連接數(shù)據(jù)庫、指定數(shù)據(jù)庫、指定集合、插入數(shù)據(jù):

mongodb存儲數(shù)據(jù)以鍵值形式, 因此在Python中使用字段插入數(shù)據(jù).

import pymongo
#連接mongodb
client = pymongo.MongoClient('mongodb://localhost:27017/')
#指定數(shù)據(jù)庫
db = client.test4
#指定集合
collection = db.students
#數(shù)據(jù)
student1 = {
 'id': '201801',
 'name': 'Jack',
 'age': 20,
 'gender': 'male'
}
student2 = {
 'id': '201802',
 'name': 'Tom',
 'age': 22,
 'gender': 'male'
}
student3 = {
 'id': '201803',
 'name': 'Rose',
 'age': 21,
 'gender': 'female'
}
student4 = {
 'id': '201804',
 'name': 'Mike',
 'age': 20,
 'gender': 'female'
}
student5 = {
 'id': '201805',
 'name': 'Ray',
 'age': 20,
 'gender': 'female'
}
student6 = {
 'id': '201806',
 'name': 'Alan',
 'age': 21,
 'gender': 'male'
}
#插入一條數(shù)據(jù)
result1 = collection.insert_one(student1)
print(result1)
print(result1.inserted_id)
# #插入多條數(shù)據(jù)
result2 = collection.insert_many([student2, student3, student4, student5, student6])
print(result2)
print(result2.inserted_ids)

運行結果:

insert方法:

5b3a1942971951218d41c02b
[ObjectId('5b3a1942971951218d41c02c'), ObjectId('5b3a1942971951218d41c02d')]

官方推薦:

<pymongo.results.InsertOneResult object at 0x7fa4cc363ec8>
5b3a1942971951218d41c02e
<pymongo.results.InsertManyResult object at 0x7fa4cc363f08>
[ObjectId('5b3a1942971951218d41c02f'), ObjectId('5b3a1942971951218d41c030')]

3. 查詢、計數(shù)、排序、偏移:

import pymongo
from bson.objectid import ObjectId
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client.test4
collection = db.students
#查詢一條數(shù)據(jù)
print('單條數(shù)據(jù)','='*50)
result = collection.find_one({'name': 'Jack'})
print(result)
print('多條數(shù)據(jù)','='*50)
#查詢多條數(shù)據(jù)
for res in collection.find({'age': {'$mod': [5, 0]}}):
 print(res)
#計數(shù)
print('計數(shù)','='*50)
count = collection.find({'age': {'$mod': [5, 0]}}).count()
print(count)
#排序
print('排序','='*50)
results = collection.find().sort('name', pymongo.ASCENDING) #升序, pymongo.DESCENDING為降序
print([result['name'] for result in results])
#偏移
print('偏移','='*50)
results = collection.find().sort('name', pymongo.ASCENDING).skip(2) #偏移2位,忽略前兩個數(shù)據(jù)
print([result['name'] for result in results])
results = collection.find().sort('name', pymongo.ASCENDING).skip(2).limit(2) #只輸出2個數(shù)據(jù)
print([result['name'] for result in results])
find({‘a(chǎn)ge': {'$mod': [5, 0]}}): 表示查找年齡取余5余0的值. 還有很多比較符號, 請百度.

運行結果:

單條數(shù)據(jù) ==================================================
{'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201801', 'name': 'Jack', 'age': 20, 'gender': 'male'}
多條數(shù)據(jù) ==================================================
{'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201801', 'name': 'Jack', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('5b3a1942971951218d41c02e'), 'id': '201804', 'name': 'Mike', 'age': 20, 'gender': 'female'}
{'_id': ObjectId('5b3a1942971951218d41c02f'), 'id': '201805', 'name': 'Ray', 'age': 20, 'gender': 'female'}
計數(shù) ==================================================
3
排序 ==================================================
['Alan', 'Jack', 'Mike', 'Ray', 'Rose', 'Tom']
偏移 ==================================================
['Mike', 'Ray', 'Rose', 'Tom']
['Mike', 'Ray']

4. 更新:

4.1  不使用$set更新數(shù)據(jù):

import pymongo
from bson.objectid import ObjectId
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client.test4
collection = db.students
#修改
condition = {'name': 'Jack'}
student = collection.find_one(condition) #獲得滿足condition的數(shù)據(jù)
print('更新前: ', student)
student['age'] = 22 #修改年齡
result = collection.update(condition, student) #將修改后的student替換condition
print('更新后', collection.find_one(condition))
#更新的返回值
print(result) #ok=1代表執(zhí)行成功, nModified代表影響的條數(shù)

運行結果:

更新前: {'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201801', 'name': 'Jack', 'age': 20, 'gender': 'male'}
更新后 {'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201801', 'name': 'Jack', 'age': 22, 'gender': 'male'}
{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}

4.2  使用$set更新數(shù)據(jù):

import pymongo
from bson.objectid import ObjectId
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client.test4
collection = db.students
#使用$set更新
condition = {'name': 'Jack'}
student = collection.find_one(condition) #獲得滿足condition的數(shù)據(jù)
print('更新前: ', student)
student['age'] = 23 #修改年齡
result = collection.update(condition, {'$set': student}) #將修改后的student替換condition, $set為重點
print('更新后', collection.find_one(condition))
#更新的返回值
print(result) #ok=1代表執(zhí)行成功, nModified代表影響的條數(shù)

運行結果:

更新前: {'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201801', 'name': 'Jack', 'age': 22, 'gender': 'male'}
更新后 {'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201801', 'name': 'Jack', 'age': 23, 'gender': 'male'}
{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}

比較使用和不適用$set更新數(shù)據(jù), 發(fā)現(xiàn)此時并沒有什么區(qū)別.

下面介紹區(qū)別所在:

4.3  區(qū)別

import pymongo
from bson.objectid import ObjectId
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client.test4
collection = db.students
#使用和不使用$set更新的區(qū)別
print('使用: ')
condition = {'name': 'Jack'}
student = collection.find_one(condition) #獲得滿足condition的數(shù)據(jù)
print('更新前: ', student)
student = {
 'id': '201803',
 'name': 'Jack',
 'age': 20,
 'gender': 'female',
 'mother': "Jack's mother"
}
result = collection.update(condition, {'$set': student}) #將修改后的student替換condition
print('更新后', collection.find_one(condition))
#更新的返回值
print(result) #ok=1代表執(zhí)行成功, nModified代表影響的條數(shù)
#分割線
print()
print('='*20, '分割線', '='*20)
print()
print('不使用: ')
condition = {'name': 'Jack'}
student = collection.find_one(condition) #獲得滿足condition的數(shù)據(jù)
print('更新前: ', student)
student = {
 'id': '201803',
 'name': 'Jack',
 'age': 20,
 'gender': 'female',
 'father': "Jack's father"
}
result = collection.update(condition, student) #將修改后的student替換condition
print('更新后', collection.find_one(condition))
#更新的返回值
print(result) #ok=1代表執(zhí)行成功, nModified代表影響的條數(shù)

運行結果:

使用:

更新前: {'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201801', 'name': 'Jack', 'age': 23, 'gender': 'male'}
更新后 {'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201803', 'name': 'Jack', 'age': 20, 'gender': 'female', 'mother': "Jack's mother"}
{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}

==================== 分割線 ====================

不使用: 
更新前: {'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201803', 'name': 'Jack', 'age': 20, 'gender': 'female', 'mother': "Jack's mother"}
更新后 {'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201803', 'name': 'Jack', 'age': 20, 'gender': 'female', 'father': "Jack's father"}
{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}

分析上面運行結果, 可以發(fā)現(xiàn)使用$set時, 若更新數(shù)據(jù)有原數(shù)據(jù)沒有的字段, 則將該字段加到原數(shù)據(jù)上(上例為新增了mother字段), 而不會刪除任何字段. 相反, 若不使用set時, 將從原數(shù)據(jù)中刪除更新數(shù)據(jù)沒有的字段, 再加上新增字段(上例為刪除了mother字段, 新增了father字段. 也可以理解為將原數(shù)據(jù)完全替換為更新數(shù)據(jù))

4.4  update_one和update_many的區(qū)別:

import pymongo
from bson.objectid import ObjectId
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client.test4
collection = db.students
#官方推薦使用
#update_one和update_many的區(qū)別
print('update_one: ')
condition = {'age': {'$gt': 20}}
result = collection.update_one(condition, {'$inc': {'age': 1}})
print(result)
print(result.matched_count, result.modified_count)
#分割線
print()
print('='*20, '分割線', '='*20)
print()
print('update_many: ')
condition = {'age': {'$gt': 20}}
result = collection.update_many(condition, {'$inc': {'age': 1}})
print(result)
print(result.matched_count, result.modified_count)

運行結果:

update_one: 
<pymongo.results.UpdateResult object at 0x7f6cace0f9c8>
1 1
==================== 分割線 ====================
update_many: 
<pymongo.results.UpdateResult object at 0x7f6cace0fa88>
3 3
12345678910
{‘a(chǎn)ge': {'$gt': 20}}為查找年齡大于20的, {‘inc': {‘a(chǎn)ge': 1}}為將年齡+1

5. 刪除:

import pymongo
from bson.objectid import ObjectId
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client.test4
collection = db.students
#刪除
result = collection.remove({'name': 'Jack'})
print(result)
#推薦使用
result = collection.delete_one({'age': {'$gt': 20}})
print(result.deleted_count)
result = collection.delete_many({'age': {'$gt': 20}})
print(result.deleted_count)

運行結果:

{'ok': 1, 'n': 1}
1
2

6. 其他

除了上述常用的之外, 還包括find_one_and_delete()查找后刪除、find_one_and_replace()查找后替換, 有興趣可以百度深入了解.

總結

以上所述是小編給大家介紹的Python與Mongodb數(shù)據(jù)庫之間的操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關文章

  • Python設計模式中的創(chuàng)建型工廠模式

    Python設計模式中的創(chuàng)建型工廠模式

    這篇文章主要介紹了Python設計模式中的創(chuàng)建型工廠模式,工廠模式即Factory?Pattern,是提供創(chuàng)建對象的最佳方式,下文小編介紹Python工廠模式的相關資料,需要的朋友可以參考一下
    2022-02-02
  • python調(diào)用fortran模塊

    python調(diào)用fortran模塊

    本文給大家介紹的是在Python中調(diào)用fortran代碼,主要是用到了f2py這個程序,十分的實用,有需要的小伙伴可以參考下
    2016-04-04
  • python rolling regression. 使用 Python 實現(xiàn)滾動回歸操作

    python rolling regression. 使用 Python 實現(xiàn)滾動回歸操作

    這篇文章主要介紹了python rolling regression. 使用 Python 實現(xiàn)滾動回歸操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 利用Python如何實現(xiàn)一個小說網(wǎng)站雛形

    利用Python如何實現(xiàn)一個小說網(wǎng)站雛形

    這篇文章主要給大家介紹了關于利用Python如何實現(xiàn)一個小說網(wǎng)站雛形的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • python實現(xiàn)文件+參數(shù)發(fā)送request的實例代碼

    python實現(xiàn)文件+參數(shù)發(fā)送request的實例代碼

    這篇文章主要介紹了python實現(xiàn)文件+參數(shù)發(fā)送request的實例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Python中字符串的格式化方法小結

    Python中字符串的格式化方法小結

    這篇文章主要介紹了Python中字符串的格式化方法小結,提到了針對Python2.x與3.x版本相異情況下的不同技巧,需要的朋友可以參考下
    2016-05-05
  • python RabbitMQ 使用詳細介紹(小結)

    python RabbitMQ 使用詳細介紹(小結)

    這篇文章主要介紹了python RabbitMQ 使用詳細介紹(小結),詳細的介紹了RabbitMQ的概念以及使用,對學習RabbitMQ有一定的幫助,非常具有實用價值,需要的朋友可以參考下
    2018-11-11
  • 使用Django和Python創(chuàng)建Json response的方法

    使用Django和Python創(chuàng)建Json response的方法

    下面小編就為大家分享一篇使用Django和Python創(chuàng)建Json response的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Python利用keras接口實現(xiàn)深度神經(jīng)網(wǎng)絡回歸

    Python利用keras接口實現(xiàn)深度神經(jīng)網(wǎng)絡回歸

    這篇文章主要為大家詳細介紹了基于Python語言中TensorFlow的Keras接口,實現(xiàn)深度神經(jīng)網(wǎng)絡回歸的方法。文中的示例代碼講解詳細,感興趣的可以了解一下
    2023-02-02
  • python爬蟲請求頁面urllib庫詳解

    python爬蟲請求頁面urllib庫詳解

    這篇文章主要介紹了python爬蟲請求頁面urllib庫詳解,python3將urllib和urllib2模塊整合并命名為urllib模塊,urllib模塊有多個子模塊,各有不同的功能,需要的朋友可以參考下
    2023-07-07

最新評論

新巴尔虎左旗| 漳浦县| 股票| 嫩江县| 鄂托克前旗| 五大连池市| 宝山区| 额尔古纳市| 临泽县| 饶平县| 汝南县| 梅河口市| 汉源县| 定日县| 曲靖市| 射阳县| 台安县| 远安县| 丰原市| 五家渠市| 仁寿县| 恩施市| 泰来县| 湟源县| 那曲县| 德格县| 丰原市| 龙南县| 廉江市| 定南县| 墨竹工卡县| 安徽省| 肥城市| 乐都县| 拜城县| 友谊县| 东乡族自治县| 锡林浩特市| 宁夏| 饶河县| 临城县|