您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“怎么用python包操作mongodb數(shù)據(jù)庫”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“怎么用python包操作mongodb數(shù)據(jù)庫”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
pip install pymongo
import pymongo # 方式一 client = pymongo.MongoClient('mongodb://localhost:27017') # 方式二 client = pymongo.MongoClient('localhost',27017) # 方式三,有密碼認(rèn)證 client = pymongo.MongoClient('localhost', 27017, username='xxx', password='xxx')
import pymongo # 連接 client = pymongo.MongoClient('mongodb://localhost:27017') # 創(chuàng)建test數(shù)據(jù)庫 db = client.test # 或者 db = client['test'] print(db)
import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') dbs = client.list_database_names()
也就是數(shù)據(jù)庫中的表
import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創(chuàng)建test數(shù)據(jù)庫 db = client.test # 創(chuàng)建表 collections = db.user # 或者 collections = db['user'] # 刪除表 collections.drop()
insert_one:插入一條數(shù)據(jù)
insert_many:插入多條數(shù)據(jù)
import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創(chuàng)建test數(shù)據(jù)庫 db = client.test # 創(chuàng)建表 collections = db.user # 創(chuàng)建文檔數(shù)據(jù) user1 = { 'name': 'autofelix', 'age': '25', 'height': '172', 'weight': '60' } user2 = { 'name': '飛兔小哥', 'age': '28', 'height': '182', 'weight': '70' } # 插入一條文檔集合 result = collections.insert_one(user1) print(result) print(result.inserted_id) # 插入多條文檔集合 result = collections.insert_many([user1, user2]) print(result) print(result.inserted_ids)
find:查詢多條數(shù)據(jù)
find_one:查詢一條數(shù)據(jù)
import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創(chuàng)建test數(shù)據(jù)庫 db = client.test # 創(chuàng)建表 collections = db.user # 查詢所有 collections.find() # 查詢最近一條 collections.find_one() # 根據(jù)條件查詢 collections.find_one({'age':25})
import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創(chuàng)建test數(shù)據(jù)庫 db = client.test # 創(chuàng)建表 collections = db.user # 跳過第一條查到的數(shù)據(jù) collections.find({'age':{'$gt':10}},['height','age']).skip(1) # limit限制查詢條數(shù) collections.find({'age':{'$gt':10}},['height','age']).limit(1) # 多條件查詢 collections.find_one({'height':{'$gt':150},'age':{'$lt':26,'$gt':10}}) # in查詢,查詢年齡在25,26,32的數(shù)據(jù) collections.find({'age':{'$in':[25, 26, 32]}}) # or查詢,查詢年齡小于等于23或者大于等于29的數(shù)據(jù) collections.find({'$or':[{'age':{'$lte':23}}, {'age':{'$gte':29}}]}) # exists查詢 collections.find({'age':{'$exists':True}}) # 正則查詢 collections.find({'name':{'$regex':r'.*auto.*'}})
import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創(chuàng)建test數(shù)據(jù)庫 db = client.test # 創(chuàng)建表 collections = db.user # 統(tǒng)計集合中總共有多少條數(shù)據(jù) collections.find().count() # 統(tǒng)計集合中年齡大于10歲的共有多少條數(shù)據(jù) collections.find({'age':{'$gt':10}}).count()
update_one:修改一條數(shù)據(jù)
update_many:修改多條數(shù)據(jù)
import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創(chuàng)建test數(shù)據(jù)庫 db = client.test # 創(chuàng)建表 collections = db.user # 修改一條數(shù)據(jù) collections.update_one({'name': 'autofelix'}, {'$set': {'name': '大神'}}) # 修改多條數(shù)據(jù) collections.update_many({'name': 'autofelix'}, {'$set': {'name': '大神'}})
delete_one:刪除一條數(shù)據(jù)
delete_many:刪除多條數(shù)據(jù)
import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創(chuàng)建test數(shù)據(jù)庫 db = client.test # 創(chuàng)建表 collections = db.user # 刪除一條數(shù)據(jù) collections.delete_one({'name': 'autofelix'}) # 刪除多條數(shù)據(jù) collections.delete_many({'name': 'autofelix'}) # 刪除所有數(shù)據(jù) collections.delete_many({})
import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創(chuàng)建test數(shù)據(jù)庫 db = client.test # 創(chuàng)建表 collections = db.user # 對字段 age 按升序排序 collections.find().sort('age') # 對字段 age 按降序排序 collections.find().sort('age', -1) # 多字段排序 collections.find().sort((('age',pymongo.ASCENDING),('height',pymongo.ASCENDING)))
讀到這里,這篇“怎么用python包操作mongodb數(shù)據(jù)庫”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。