python使用pymongo與MongoDB基本交互操作示例
本文實(shí)例講述了python使用pymongo與MongoDB基本交互操作。分享給大家供大家參考,具體如下:
本文內(nèi)容: pymongo的使用: 安裝模塊 導(dǎo)入模塊 連接mongod 獲取切換數(shù)據(jù)庫(kù) 選擇集合 CRUD操作首發(fā)時(shí)間:2018-03-18 20:11
pymongo的使用: 安裝模塊:pip3 pymongo 導(dǎo)入模塊:
import pymongo 連接mongod:
conn=pymongo.MongoClient(host='localhost',port=27017) 獲取切換數(shù)據(jù)庫(kù):
# db=conn.School #獲取School數(shù)據(jù)庫(kù) db=conn[’School’] #獲取School數(shù)據(jù)庫(kù) 選擇集合:
# collection=db.teacher#選擇teacher集合 collection=db[’teacher’]#選擇teacher集合 CRUD操作:【pymongo的方法與mongo的命令基本一致,名字類似的功能也類似,參數(shù)可以參考mongo的命令,以及源碼說明】 查看文檔: find():返回值是一個(gè)Cursor類型的,需要迭代這個(gè)返回值才能獲取結(jié)果 find_one():返回值是查找結(jié)果
importpymongo conn=pymongo.MongoClient(host='localhost',port=27017) db=conn[’School’] collection=db[’teacher’] rel=collection.find() print([r for r in rel]) rel=collection.find({'name':'Alex'}) print([r for r in rel]) # rel=collection.find({'age':{'$gt':20}}) rel=collection.find({'$or':[{'name':'Amy'},{'name':'Alex'}]}) print([r for r in rel]) rel=collection.find_one({'name':'jack'}) print(rel) print(rel[’name’])#單個(gè)文檔情況下可用來取出指定值 conn.close() 插入文檔: insert():插入單條文檔,可選,多條文檔使用列表插入,已經(jīng)不建議使用 insert_one():插入單條文檔 insert_many():插入多條文檔
importpymongo conn=pymongo.MongoClient(host='localhost',port=27017) db=conn[’School’] collection=db[’teacher’] collection.insert({'name':'Job','course':'career'}) # col.insert(document)#**DEPRECATED** - Use :meth:`insert_one` or :meth:`insert_many` instead.#insert是不推薦用了,建議使用insert_one,insert_many collection.insert_one({'name':'Job1','course':'career1'}) t1={'name':'Job2','course':'career2'} t2={'name':'Job3','course':'career3'}collection.insert_many([t1,t2])conn.close() 修改文檔: update():修改單條或多條文檔,由選項(xiàng)multi決定,但已不推薦使用該方法,建議使用update_one()、update_many() update_one():修改單條文檔 update_many():修改多條文檔
importpymongo conn=pymongo.MongoClient(host='localhost',port=27017) db=conn[’School’] collection=db[’teacher’] # rel=collection.update({'name':'Job1'},{ '$set':{'name':'Bob'}})#不推薦使用#collection.update_one({'name':'Job'},{ '$set':{'name':'Bob'}}) collection.update_many({'name':'Job1'},{ '$set':{'name':'Bob'}})conn.close() 刪除文檔: remove():刪除指定文檔,但已經(jīng)不建議使用,建議使用delete_one和delete_many delete_one(): 刪除符合條件的一條文檔 delete_many():刪除符合條件的所有文檔
importpymongo conn=pymongo.MongoClient(host='localhost',port=27017) db=conn[’School’] collection=db[’teacher’] # collection.remove({'name':'Bob'})#collection.delete_one({'name':'Bob2'}) collection.delete_many({'name':'Job3'})conn.close()
想了解更多,可以參考pymongo官方文檔:http://api.mongodb.com/python/current/api/pymongo/
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫(kù)操作技巧匯總》、《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. XML入門的常見問題(二)2. jsp cookie+session實(shí)現(xiàn)簡(jiǎn)易自動(dòng)登錄3. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式4. asp中response.write("中文")或者js中文亂碼問題5. JSP 中request與response的用法詳解6. asp取整數(shù)mod 有小數(shù)的就自動(dòng)加17. ASP基礎(chǔ)知識(shí)Command對(duì)象講解8. css代碼優(yōu)化的12個(gè)技巧9. phpstudy apache開啟ssi使用詳解10. CSS3中Transition屬性詳解以及示例分享
