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