最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python操作MongoDB增刪改查代碼示例

 更新時間:2022年12月21日 14:25:59   作者:笑得好美  
這篇文章主要介紹了Python操作MongoDB增刪改查代碼示例,需要的朋友可以參考下

python安裝操作mongodb的模塊pymongo

pip install pymongo

python連接mongodb

myclient=pymongo.MongoClient(host='127.0.0.1',port=27017)   #指定主機(jī)和端口號創(chuàng)建客戶端

查看MongoDB中存在的數(shù)據(jù)庫

dblist=myclient.list_database_names() 
print(dblist)

數(shù)據(jù)庫使用:有則直接使用,沒有的話數(shù)據(jù)庫只有在內(nèi)容插入后才會自動創(chuàng)建!

mydb=myclient['dbtest']
print(mydb)

查看當(dāng)前數(shù)據(jù)庫中的表

collist = mydb. list_collection_names()
print(collist)

使用表(集合)

mycol=mydb['t1']#有則直接使用,沒有的話集合只有在內(nèi)容插入后才會創(chuàng)建!
print(mycol)

MongoDB中數(shù)據(jù)表的操作

1.Python添加MongoDB數(shù)據(jù)

(1)insert()----單條和多條插入:MongoDB3.2版本自之前
    db.collection.insert({})
    db.collection.insert([{},{}...])
(2)3.2之后官方推薦:
    insert_one()----單條插入
        db.collection.insert_one({})
    insert_many()----多條插入
        db.collection.insert_many([{},{}...])

res1=mycol.insert([{'name':'a','age':20,'gender':1}])
print(res1)
res2=mycol.insert([{'name':'b','age':21,'gender':0},{'name':'c'}])
print(res2)
???????mydict = { "name": "Google", "alexa": "1", "url": "https://www.google.com" }
mylist = [
  { "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" },
  { "name": "QQ", "alexa": "101", "url": "https://www.qq.com" },
  { "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" },
  { "name": "知乎", "alexa": "103", "url": "https://www.zhihu.com" },
  { "name": "Github", "alexa": "109", "url": "https://www.github.com" }
]
res3=mycol.insert_one(mydict)
print(res3)
res4=mycol.insert_many(mylist)
print(res4)

2.Python查詢MongoDB數(shù)據(jù)

(1)find()----查詢符合條件的所有數(shù)據(jù)
    db.collection.find(查詢條件)
(2)find_one()----查詢符合條件的第一條數(shù)據(jù)
    db.collection.find_one(查詢條件)

all=mycol.find()
# all=mycol.find({'age':21})
print(all)
for doc in all:
    print(doc)
# one=mycol.find_one()
one=mycol.find_one({'age':21})
print(one)

3.Python修改MongoDB數(shù)據(jù)(不存在即創(chuàng)建)

(1)update()---只能修改查詢出的第一條數(shù)據(jù)
    db.coolection.update({查詢條件},{$修改器:{修改值}})
    修改器:認(rèn)定當(dāng)前修改的類型 $set設(shè)置類型修改器 強(qiáng)制修改某字段的值
(2)3.2之后官方推薦:
    update_one()----修改查詢結(jié)果的第一條數(shù)據(jù)
        db.colleciton.update_one({查詢條件},{$修改器:{修改值}})
    update_many()----修改查詢結(jié)果所有數(shù)據(jù)
        db.colleciton.update_many({查詢條件},{$修改器:{修改值}})

res5=mycol.update({'name':'c'},{'$set':{'age':23}})
print(res5)
res6=mycol.update_one({'name':'d'},{'$set':{'age':30,'gender':1}})
print(res6)
res7=mycol.update_many({'name':'d'},{'$set':{'age':22}})
print(res7)

4.Python刪除MongoDB數(shù)據(jù)

(1)remove()----不在推薦使用
    db.collection.remove(查詢條件)----刪除符合查詢結(jié)果的所有數(shù)據(jù)
(2)3.2之后官方推薦:
    delete_one()----刪除查詢結(jié)果的第一條數(shù)據(jù)
        db.colleciton.delete_one(查詢條件)
    delete_many()----刪除查詢結(jié)果的所有數(shù)據(jù)
        db.colleciton.delete_many(查詢條件)

res8=mycol.remove({'age':22})
print(res8)
res9=mycol.delete_one({'name':'c'})
print(res9)
res10=mycol.delete_many({'name':'a'})
print(res10)

5.Python操作MongoDB數(shù)據(jù)排序+跳躍+范圍

對查詢結(jié)果進(jìn)行排序、跳躍取值、范圍截取
(1)sort(filed,pymongo.ASCENDING/pymongo.DESCENDING)----對查詢結(jié)果升序/降序排序
        db.collection.find({}).sort()
(2)skip(num)----對查詢結(jié)果進(jìn)行跳躍取值
        db.collection.find({}).skip()
(3)limit(num)----對查詢結(jié)果進(jìn)行范圍截取
        db.collection.find({}).limit()
(4)優(yōu)先級:sort>skip>limit,與使用時的順序無關(guān)
        db.collection.find({}).sort().skip().limit()
        db.collection.find({}).limit().sort().skip()
        db.collection.find({}).skip().sort().limit()

print('----------------------------')
alldata1=mycol.find({}).sort('age',pymongo.ASCENDING)#升序
for i in alldata1:
    print(i)
print('----------------------------')
alldata2=mycol.find({}).skip(8)
for i in alldata2:
    print(i)
print('----------------------------')
alldata3=mycol.find({}).limit(2)
for i in alldata3:
    print(i)

6.查詢關(guān)鍵字

(1)$and----并列查詢
        db.collection.find({'$and':[{},{}...]})
(2)$or----或條件查詢
        db.collection.find({'$or':[{},{}...]})
(3)$in----范圍查詢
        db.collection.find({field:{'$in':['',''...]}})
(4)$all----子集查詢
         db.collection.find({field:{'$all':['',''...]}})

print('+++++++++++++++++++++++++++++++')
res11=mycol.find({"name":'c'})
for i in res11:
    print(i)
print('+++++++++++++++++++++++++++++++')
res12=mycol.find({'$and':[{'name':'c'},{'age':20}]})
for i in res12:
    print(i)
print('+++++++++++++++++++++++++++++++')
res13=mycol.find({'$or':[{'name':'c'},{'name':'b'}]})
for i in res13:
    print(i)
print('+++++++++++++++++++++++++++++++')
res14=mycol.find({'name':{'$in':['c','b']}})
for i in res14:
    print(i)
print('+++++++++++++++++++++++++++++++')
res15=mycol.find({'hobby':{'$all':['run','eat','swim']}})
res15=mycol.find({'hobby':{'$all':['run','eat']}})
for i in res15:
    print(i)

7.查詢條件操作符(一般用于數(shù)字比較)

(1)$lt----小于
    db.collection.find({field:{'$lt':value}})
(2)$gt----大于
    db.collection.find({field:{'$gt':value}})
(3)$eq----等于
    db.collection.find({field:{'$eq':value}})
(4)$lte----小于等于
    db.collection.find({field:{'$lte':value}})
(5)$gte----大于等于
    db.collection.find({field:{'$gte':value}})
(6)$ne----不等于
    db.collection.find({field:{'$ne':value}})

# mycol.insert_many([{'name':'d','age':18},{'name':'e','age':22},{'name':'f','age':32},])
print("**************************************")
res16=mycol.find({'age':{'$lt':21}})
for i in res16:
    print(i)
print("**************************************")
res17=mycol.find({'age':{'$gt':21}})
for i in res17:
    print(i)
print("**************************************")
res18=mycol.find({'age':{'$eq':21}})
for i in res18:
    print(i)
print("**************************************")
res19=mycol.find({'age':{'$lte':21}})
for i in res19:
    print(i)
print("**************************************")
res20=mycol.find({'age':{'$gte':21}})
for i in res20:
    print(i)
print("**************************************")
res21=mycol.find({'age':{'$ne':21}})
for i in res21:
    print(i)

8.$修改器 + $ 字符特殊用法

(1)$set----修改某個字段的值
(2)$unset---刪除字段
(3)$inc----引用增加(先引用 后增加)
(4)針對數(shù)組操作:
    ①$push----在Array的最后一個位置中增加一個數(shù)據(jù)
    ②$pushAll----在Array的最后一個位置中增加多個數(shù)據(jù)
    ③$pull ----刪除Array中的指定單個元素
    ④$pullAll ----刪除Array中的指定多個元素
    ⑤$pop----刪除Array中的第一個或者最后一個元素 正數(shù)是倒序刪除 負(fù)數(shù)是正序刪除
    ⑥$----存儲當(dāng)前(Array)符合條件的元素下標(biāo)索引 ,只能存儲最外層的 索引位置

# mycol.update_one({'name':'c','age':20},{'$set':{'hobby':['swim,dump','sing']}})
# mycol.update_one({'$and':[{'name':'c'},{'age':20}]},{"$unset":{'hobby':[1,2]}})
# mycol.update_many({'name':{'$in':['d','e','f']}},{'$inc':{'age':2}})
# mycol.update_one({'name':'b'},{'$push':{'hobby':['swim','sing']}})
# mycol.update({'name':'c'},{'$pushAll':{'hobby':['sing','scrapy']}})#實際測試無法使用,報錯:Unknown modifier: $pushAll
# mycol.update_many({'name':'b'},{'$pullAll':{'hobby':['swim','play']}})#實際測試每次只刪除一個元素
# mycol.update_one({'hobby':'run'},{'$pull':{'hobby':'eat'}})
# mycol.update_many({'hobby':'run'},{'$pop':{'hobby':1}})
# mycol.update_many({'hobby':'run'},{'$pop':{'hobby':-1}})
# mycol.update_many({'name':'c','age':{'$ne':20}},{'$set':{'hobby':['swim','sing']}})
# mycol.update_many({'hobby':'run'},{'$push':{'hobby':'swim'}})

本文主要講解了Python操作MongoDB增刪改查代碼實例,更多關(guān)于Python操作MongoDB的知識請查看下面的相關(guān)鏈接

相關(guān)文章

  • Python安裝及Pycharm安裝使用教程圖解

    Python安裝及Pycharm安裝使用教程圖解

    這篇文章主要介紹了Python安裝以及Pycharm安裝使用教程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • python實現(xiàn)問號表達(dá)式(?)的方法

    python實現(xiàn)問號表達(dá)式(?)的方法

    這篇文章主要介紹了python實現(xiàn)問號(?)表達(dá)式的方法,大家參考使用吧
    2013-11-11
  • 使用python實現(xiàn)回文數(shù)的四種方法小結(jié)

    使用python實現(xiàn)回文數(shù)的四種方法小結(jié)

    今天小編就為大家分享一篇使用python實現(xiàn)回文數(shù)的四種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 小小聊天室Python代碼實現(xiàn)

    小小聊天室Python代碼實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了小小聊天室Python具體的實現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Python netmiko模塊的使用

    Python netmiko模塊的使用

    這篇文章主要介紹了Python netmiko模塊的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Python3實現(xiàn)騰訊云OCR識別

    Python3實現(xiàn)騰訊云OCR識別

    這篇文章主要為大家詳細(xì)介紹了Python3實現(xiàn)騰訊云OCR識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • OpenCV+python實現(xiàn)實時目標(biāo)檢測功能

    OpenCV+python實現(xiàn)實時目標(biāo)檢測功能

    這篇文章主要介紹了OpenCV+python實現(xiàn)實時目標(biāo)檢測功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • python 監(jiān)聽salt job狀態(tài),并任務(wù)數(shù)據(jù)推送到redis中的方法

    python 監(jiān)聽salt job狀態(tài),并任務(wù)數(shù)據(jù)推送到redis中的方法

    今天小編就為大家分享一篇python 監(jiān)聽salt job狀態(tài),并任務(wù)數(shù)據(jù)推送到redis中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python在linux系統(tǒng)下獲取系統(tǒng)內(nèi)存使用情況的方法

    python在linux系統(tǒng)下獲取系統(tǒng)內(nèi)存使用情況的方法

    這篇文章主要介紹了python在linux系統(tǒng)下獲取系統(tǒng)內(nèi)存使用情況的方法,涉及Python在Linux平臺下獲取系統(tǒng)硬件信息的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • 如何在ubuntu中切換使用不同版本的python

    如何在ubuntu中切換使用不同版本的python

    在ubuntu環(huán)境下進(jìn)行嵌入式開發(fā),我們在進(jìn)行不同的項目開發(fā)時,可能會遇到python環(huán)境不統(tǒng)一的情況,下面這篇文章主要給大家介紹了關(guān)于如何在ubuntu中切換使用不同版本的python的相關(guān)資料,需要的朋友可以參考下
    2023-02-02

最新評論

襄樊市| 新干县| 德惠市| 锦州市| 江孜县| 桑植县| 乐亭县| 宁河县| 加查县| 怀集县| 吉首市| 辽宁省| 革吉县| 麟游县| 长沙县| 苗栗县| 林州市| 诸暨市| 浦县| 京山县| 凯里市| 衡南县| 靖宇县| 吉林省| 疏勒县| 浦东新区| 花莲县| 兴仁县| 屯昌县| 中江县| 安仁县| 株洲县| 阿坝县| 色达县| 南京市| 张家界市| 乌拉特前旗| 大英县| 巨野县| 固安县| 义马市|