日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python也能操作MongoDB數(shù)據(jù)庫

大家好,我是Python進(jìn)階者。

成都創(chuàng)新互聯(lián)主營綏中網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,手機(jī)APP定制開發(fā),綏中h5微信小程序開發(fā)搭建,綏中網(wǎng)站營銷推廣歡迎綏中等地區(qū)企業(yè)咨詢

前言

作為非關(guān)系數(shù)據(jù)庫的代表--Mongo,可以說是讓人又愛又恨,讓人愛的是它的便捷性,讓人恨的是它的配置,實(shí)在是坑多。那么今天我們就來深入剖析它吧。

一、下載并導(dǎo)入Python 連接Mongo的模塊

 
 
 
 
  1. pip install pymongo 
  2. from pymongo import MongoClient 

二、連接Mongo數(shù)據(jù)庫

1.普通登錄,又稱游客登陸,安全等級低

 
 
 
 
  1. MongoClient('mongodb://localhost:27017/') 

2.用戶密碼登陸,安全等級高

 
 
 
 
  1. MongoClient('mongodb://hwzjj:123456@localhost:27017/hw') 

這里連接到了用戶名為hwzjj,密碼為123456的用戶。

三、執(zhí)行插入操作

為了安全,我們使用用戶名和密碼登陸,然后創(chuàng)建一個(gè)集合,不知道大家對Mongo創(chuàng)建集合還有沒有印象,反正小編還有,廢話不多說,先創(chuàng)建兩個(gè)集合。

 
 
 
 
  1. db.createCollection(name='student',option={capped:true,autoIndexId:true,size:100,max:1000}) 
  2. db.createCollection(name='teacher',option={capped:true,autoIndexId:true,size:200,max:2000}) 

這樣就創(chuàng)建了一student和teacher的集合了。然后我們再來顯示一下所有的集合名:

 
 
 
 
  1. show collections; 

然后我們往集合里插入數(shù)據(jù),在Mongo中是這樣插入的:

可以看到我們成功插入了兩條數(shù)據(jù),接下來我們利用Python來插入數(shù)據(jù)。

1.直接使用創(chuàng)建好的集合插入數(shù)據(jù)

 
 
 
 
  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw') 連接數(shù)據(jù)庫 
  3. db=client['hw']        選擇數(shù)據(jù)庫hw 
  4. coll=db['student']     選擇集合 
  5. res={'id':'0003','name':'任性','age':43} 
  6. first=coll.insert_one(res)  將數(shù)據(jù)插入到集合中 
  7. print(first.inserted_id)   打印插入數(shù)據(jù)的id(每個(gè)插入數(shù)據(jù)都會有) 

2.自己創(chuàng)建集合插入數(shù)據(jù)

 
 
 
 
  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw') 
  3. db=client['hw'] 
  4. db.create_collection('teacher')  創(chuàng)建集合 
  5. res={'id':'0001','name':'boy','age':36} 
  6. last=db.student.insert_one(res)  插入數(shù)據(jù) 
  7. print(last.inserted_id) 打印id 

3.插入多條數(shù)據(jù)

 
 
 
 
  1. import random 
  2. from pymongo import MongoClient 
  3. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw') 
  4. db=client['hw'] 
  5. coll=db['student'] 
  6. def get(): 
  7.     for y in range(100000): 
  8.         data={'id':y,'name':'user--'+str(y),'age':random.choice(range(100))} 
  9.         yield data 
  10. for y in get(): 
  11.     coll.insert(y) 

同樣是插入十萬個(gè)數(shù)據(jù), 不過數(shù)據(jù)卻是比Mysql慢一點(diǎn),可自行測試。

注:執(zhí)行插入操作時(shí),Insert最多可插入四條同樣的記錄。

四、執(zhí)行更改操作

仍舊是先要獲取集合,然后對集合中的內(nèi)容進(jìn)行修改。

1.更新匹配到的第一條數(shù)據(jù)

 
 
 
 
  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw') 
  3. db=client['hw'] 
  4. coll=db['student'] 
  5. coll.update_one({'name':'user--10'},{'$set':{'name':'用戶已注銷'}}) 更新匹配到的第一條數(shù)據(jù) 

2.更新匹配到的所有數(shù)據(jù)

我們創(chuàng)建四個(gè)一樣的數(shù)據(jù),將程序執(zhí)行四次即可:

 
 
 
 
  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw') 
  3. db=client['hw'] 
  4. coll=db['student'] 
  5. coll.insert({'id':'111','name':'hw','age':43}) 

可以看到生成了四個(gè)同樣的記錄,當(dāng)然了,只能生成最多4條記錄。然后我們?nèi)繉⑺鼈償?shù)據(jù)修改。

 
 
 
 
  1. coll.update({'name':'hw'},{'$set':{'name':'用戶已注冊'}}) 

五、執(zhí)行刪除操作

1.刪除所有符合條件的數(shù)據(jù)

 
 
 
 
  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw') 
  3. db=client['hw'] 
  4. coll=db['student'] 
  5. coll.insert({'id':'111','name':'hw','age':43}) 插入數(shù)據(jù) 
  6. coll.remove({'name':'hw'}) 刪除所有name 為hw的數(shù)據(jù),注意不要以id為條件來刪除,會報(bào)錯(cuò) 
  7. coll.delete_many({'name':'hw'}) 跟上者功能一樣 

2.刪除所有符合條件的第一條數(shù)據(jù)

 
 
 
 
  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw') 
  3. db=client['hw'] 
  4. coll=db['student'] 
  5. coll.insert({'id':'111','name':'hw','age':43}) 
  6. coll.delete_one({'name':'hw'}) 刪除符合條件的第一條數(shù)據(jù) 

六、執(zhí)行查詢操作

1.查詢符合條件的第一條數(shù)據(jù)

2.查詢符合條件的所有數(shù)據(jù)

3.查找后刪除

4.查找后替換

5.查找后更新

6.統(tǒng)計(jì)符合條件的記錄數(shù)量

 
 
 
 
  1. coll.find().count() # 記錄符合條件的數(shù)量 

7.符合條件的數(shù)據(jù)的排序

 
 
 
 
  1. coll.find().sort('name', pymongo.ASCENDING) # 升序排序 DESCENDING 降序排序 

8.符合條件數(shù)量中跳過

 
 
 
 
  1. https://mp.weixin.qq.com/s/34t_u-JxL3HFXvEdtgFQEg#:~:text=coll.find().sort(%27name%27%2C%20pymongo.ASCENDING).skip(1)%20%23%20%E8%B7%B3%E8%BF%87%E4%B8%80%E4%B8%AA%E8%AE%B0%E5%BD%95 

9.限制符合條件輸出數(shù)量

 
 
 
 
  1. coll.find().sort('name', pymongo.ASCENDING).limit(2) # 輸出兩個(gè)符合條件的記錄 

10.通過Id來查找

每個(gè)插入的數(shù)據(jù)都會生成一個(gè)id,貌似被加密了,前面我們已經(jīng)和它打過交道了,下面來看下它的使用。

 
 
 
 
  1. from bson.objectid import ObjectId 
  2. find_one({'_id': ObjectId(id_name)}) 

七、索引操作

1.創(chuàng)建索引

可以看到有兩個(gè)索引,一個(gè)是Mongo自動創(chuàng)建的在id上的索引,另一個(gè)是剛剛創(chuàng)建在name上的索引。

2.獲取索引

 
 
 
 
  1. for y in coll.list_indexes(): # 獲取所有索引 
  2.   print(y) 

3.刪除索引

可以看到剛剛的索引name已經(jīng)被刪除了,而且只有一條數(shù)據(jù)了,那么有人就問了,為何不把_id一起刪除,很抱歉,這個(gè)是刪不了的。

八、總結(jié)

通過本章對Pymongo的學(xué)習(xí),相信你已經(jīng)可以勝任日常一些開發(fā)了,Pymongo中還有很多值得學(xué)習(xí)的地方,值得你去推敲,在這里就不一一列舉了,希望本文能帶大家零基礎(chǔ)毫無壓力入門Pymongo。


本文標(biāo)題:Python也能操作MongoDB數(shù)據(jù)庫
文章鏈接:http://www.dlmjj.cn/article/dpihijp.html