您好,登錄后才能下訂單哦!
這篇文章主要講解了python使用pymongo與MongoDB基本交互的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
pymongo的使用:
pip3 pymongo
import pymongo
conn=pymongo.MongoClient(host="localhost",port=27017)
# db=conn.School #獲取School數(shù)據(jù)庫(kù) db=conn['School'] #獲取School數(shù)據(jù)庫(kù)
# collection=db.teacher#選擇teacher集合 collection=db['teacher']#選擇teacher集合
import pymongo 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è)文檔情況下可用來(lái)取出指定值 conn.close()
import pymongo 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()
import pymongo 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()
import pymongo 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/
看完上述內(nèi)容,是不是對(duì)python使用pymongo與MongoDB基本交互的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。