Python操作MongoDB數(shù)據(jù)庫的方法示例
更新時間:2018年01月04日 14:59:52 作者:chengqiuming
這篇文章主要介紹了Python操作MongoDB數(shù)據(jù)庫的方法,結(jié)合實例形式分析了Python命令行模式下操作MongoDB數(shù)據(jù)庫實現(xiàn)連接、查找、刪除、排序等相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了Python操作MongoDB數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
>>> import pymongo
>>> client=pymongo.MongoClient ('localhost',27017)
>>> db=client.students
>>> db.collection_names()
['students']
>>> students=db.students
>>> students.find()
<pymongo.cursor.Cursor object at 0x0000017A74305FD0>
>>> for item in students.find():
print(item)
{'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 18.0, 'sex': 'male'}
>>> wangwu={'name':'wangwu','age':20,'sex':'male'}
>>> students.insert(wangwu)
ObjectId('593a7c5fedb5a1abeb757052')
>>> for item in students.find({'name':'wangwu'}):
print(item)
{'_id': ObjectId('593a7c5fedb5a1abeb757052'), 'name': 'wangwu', 'age': 20, 'sex': 'male'}
>>> students.find_one()
{'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 18.0, 'sex': 'male'}
>>> students.find_one({'name':'wangwu'})
{'_id': ObjectId('593a7c5fedb5a1abeb757052'), 'name': 'wangwu', 'age': 20, 'sex': 'male'}
>>> students.find().count()
2
>>> students.remove({'name':'wangwu'})
{'ok': 1, 'n': 1}
>>> for item in students.find():
print(item)
{'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 18.0, 'sex': 'male'}
>>> students.find().count()
1
>>> students.create_index([('name',pymongo.ASCENDING)])
'name_1'
>>> students.update({'name':'zhangsan'},{'$set':{'age':25}})
{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}
>>> students.find_one()
{'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 25, 'sex': 'male'}
>>> students.update({'age':25},{'$set':{'sex':'Female'}})
{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}
>>> students.remove()
{'ok': 1, 'n': 1}
>>> students.find().count()
0
>>> zhangsan={'name':'zhangsan','age':25,'sex':'Male'}
>>> lisi={'name':'lisi','age':21,'sex':'Male'}
>>> wangwu={'name':'wangwu','age':22,'sex':'Female'}
>>> students.insert_many([zhangsan,lisi,wangwu])
<pymongo.results.InsertManyResult object at 0x0000017A749FC5E8>
>>> for item in students.find().sort('name',pymongo.ASCENDING):
print(item)
{'_id': ObjectId('593a806bedb5a1abeb757054'), 'name': 'lisi', 'age': 21, 'sex': 'Male'}
{'_id': ObjectId('593a806bedb5a1abeb757055'), 'name': 'wangwu', 'age': 22, 'sex': 'Female'}
{'_id': ObjectId('593a806bedb5a1abeb757053'), 'name': 'zhangsan', 'age': 25, 'sex': 'Male'}
>>> for item in students.find().sort([('sex',pymongo.DESCENDING),('name',pymongo.ASCENDING)]):
print(item)
{'_id': ObjectId('593a806bedb5a1abeb757054'), 'name': 'lisi', 'age': 21, 'sex': 'Male'}
{'_id': ObjectId('593a806bedb5a1abeb757053'), 'name': 'zhangsan', 'age': 25, 'sex': 'Male'}
{'_id': ObjectId('593a806bedb5a1abeb757055'), 'name': 'wangwu', 'age': 22, 'sex': 'Female'}
>>>
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
您可能感興趣的文章:
- Python實現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法示例
- python讀取json文件并將數(shù)據(jù)插入到mongodb的方法
- python連接mongodb操作數(shù)據(jù)示例(mongodb數(shù)據(jù)庫配置類)
- Python操作MongoDB數(shù)據(jù)庫PyMongo庫使用方法
- python操作mongodb根據(jù)_id查詢數(shù)據(jù)的實現(xiàn)方法
- Python常見MongoDB數(shù)據(jù)庫操作實例總結(jié)
- python如何實現(xiàn)excel數(shù)據(jù)添加到mongodb
- Python實現(xiàn)批量讀取圖片并存入mongodb數(shù)據(jù)庫的方法示例
- Python實現(xiàn)將數(shù)據(jù)框數(shù)據(jù)寫入mongodb及mysql數(shù)據(jù)庫的方法
- Python使用pymongo庫操作MongoDB數(shù)據(jù)庫的方法實例
- Python MongoDB 插入數(shù)據(jù)時已存在則不執(zhí)行,不存在則插入的解決方法
相關(guān)文章
關(guān)于Tensorflow使用CPU報錯的解決方式
今天小編就為大家分享一篇關(guān)于Tensorflow使用CPU報錯的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python將PDF轉(zhuǎn)換為HTML的實現(xiàn)方法
PDF文件是共享和分發(fā)文檔的常用選擇,但提取和再利用PDF文件中的內(nèi)容可能會非常麻煩,本文重點介紹如何在Python程序中將PDF轉(zhuǎn)換為HTML,文中有詳細的代碼示例,需要的朋友可以參考下2024-03-03

