python操作mongo(2)
2019獨角獸企業重金招聘Python工程師標準>>>
更新數據
你可以通過調用**update_one()和update_many()**方法來更新集合collection中特定的文檔.update_one()一次只能更新一個內容。使用update_many()可以一次性更新多個文檔內容。
預備條件
from pymongo import MongoClientclient = MongoClient() db = client.test更新高等級字段
下面的操作是更新第一個匹配name為juni的文檔,通過$set操作來更新cuisine和currentDate字段(更新為當前的時間)。
from pymongo import MongoClientclient=MongoClient() db=client.testresult = db.restaurants.update_one({"name": "Juni"},{"$set": {"cuisine": "American (New)"},"$currentDate": {"lastModified": True}} )調用 update_one操作返回的一個UpdateResult對象,表示匹配的文件計數 modified_count表示的是當前修改的總數
result.matched_count將上面的值進行輸出得到的值為1
更新一個嵌入的文檔
下面的操作是更新一個嵌入的文檔中的一個字段,通過使用.符號來訪問相應的字段名。
result = db.restaurants.update_one({"restaurant_id": "41156888"},{"$set": {"address.street": "East 31st Street"}} )上面的操作result表示的是修改的文檔數量
更新多個文檔
update_one()一次只能更新一個文檔,可以通過使用update_many()方法來同時更新多個文件,下面的操作匹配文檔當中所有address.zipcode為10016和cuisine為Other的字段,并且設置其cusine為“Category,并且將lastModified更新為當前的時間.
result = db.restaurants.update_many({"address.zipcode": "10016", "cuisine": "Other"},{"$set": {"cuisine": "Category To Be Determined"},"$currentDate": {"lastModified": True}} )上面方法返回一個UpdateResult對象,該對象里面含有匹配的文件個數以及更改文件的計數器。下面的語句返回的為20,表示上面的操作更新了二十條記錄
result.matched_count
替換文檔
調用update()方法只是更新特定的字段,而使用replace_one或者replace_many(),將整個匹配的內容更改為你輸入的內容。 比如我們原本有下面的內容
{'cuisine': 'Italian',
'restaurant_id': '41704620',
'name': 'Vella',
'_id': ObjectId('5704d3c3a75b1775d3b2583b'),
'borough': 'Manhattan',
'address': {'coord': [-73.9557413, 40.7720266], 'building': '1480', 'zipcode': '10075', 'street': '2 Avenue'}, 'grades': [{'date': datetime.datetime(2014, 10, 1, 0, 0), 'score': 11, 'grade': 'A'},
{'date': datetime.datetime(2014, 1, 16, 0, 0), 'score': 17, 'grade': 'B'}]}
我們通過調用 下面的語句
result = db.restaurants.replace_one({"restaurant_id": "41704620"},{"name": "Vella 2","address": {"coord": [-73.9557413, 40.7720266],"building": "1480","street": "2 Avenue","zipcode": "10075"}} )這里我們再次查詢的時候反正已經不能通過restaurant_id來進行查詢了,因為該記錄已經完成更新為非典所給出的字段了,這里查詢的內容如下:
{'_id': ObjectId('5704d3c3a75b1775d3b2583b'),
'name': 'Vella 2',
'address': {'zipcode': '10075', 'building': '1480', 'street': '2 Avenue', 'coord': [-73.9557413, 40.7720266]}}
默認的當一個update操作沒有任何匹配的數據時,mongo什么都不會去做。但是我們可以通過設置upsert=true,這樣一旦匹配不到任何的數據時,會將當前的數據當成新的數據插入到collection當中
##移除數據 移除同樣提供了兩個方法一個是delete_one()另一個并是delete_many()。
- delete_one 一次只刪除一條記錄
- delete_many() 可以刪除多條匹配的記錄
###移除所有匹配的內容 下面的操作是刪除所有borough為Manhattan
result = db.restaurants.delete_many({"borough": "Manhattan"})上面的操作返回的是一個 DeleteResult 的對象,該對象記錄的是匹配的數量和刪除的數量
result.deleted_count 輸出的結果為1263行,表示刪除的總數
移除所有的內容
result = db.restaurants.delete_many({})上面不帶任何條件的執行的結果,刪除所有的內容 通過打印result.deleted_count結果為15100,表示刪除了15100行記錄
###刪除Collection 下面的語句直接將restaurants這個集合刪除
db.restaurants.drop()
##數據集合 在mongodb當中 通過aggregate()進行分組
db.collection.aggregate([<stage1>, <stage2>, ...])
###通過字段進行分組,并且計算該字段的總數
我們使用$group來指定指定的鍵(key)進行分組。在$group當中主要通過_id字段來進行分組。通過$group來訪問field路徑。在使用$前綴的字段名稱來進行訪問.
通過accumulators來計算每一組的數據數。下面的示例就是通過borough字段來進行分組,并且使用$sum來進行分組的 總數的累加
cursor = db.restaurants.aggregate([{"$group": {"_id": "$borough", "count": {"$sum": 1}}}] ) for document in cursor:print(document)上面的代碼執行之后的結果如下面所示
{'_id': None, 'count': 1}
{'_id': 'Missing', 'count': 51}
{'_id': 'Staten Island', 'count': 969}
{'_id': 'Queens', 'count': 5656}
{'_id': 'Bronx', 'count': 2338}
{'_id': 'Brooklyn', 'count': 6086}
過程分組集合
我們使用$match篩選相應的集合。$match使用的是mongodb 查詢語法。 下面的示例是通過$match進行過濾,只有同時滿足borough為Queens和curisine為Brazilian的條件才會參與分組。 address.zipcodewdt 做為分組的字段,并且$sum進行最終的累加匯總
cursor = db.restaurants.aggregate([{"$match": {"borough": "Queens", "cuisine": "Brazilian"}},{"$group": {"_id": "$address.zipcode", "count": {"$sum": 1}}}] )最終的結果如下所示
{'count': 1, '_id': '11377'}
{'count': 1, '_id': '11368'}
{'count': 2, '_id': '11101'}
{'count': 3, '_id': '11106'}
{'count': 1, '_id': '11103'}
轉載于:https://my.oschina.net/u/215677/blog/653927
總結
以上是生活随笔為你收集整理的python操作mongo(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 注册团队博客地址
- 下一篇: iOS开发之简单画板实现