99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python也能操作MongoDB數(shù)據(jù)庫
Python也能操作MongoDB數(shù)據(jù)庫
2021-08-06
收藏

來源:Python爬蟲與數(shù)據(jù)挖掘

作者:Python進(jìn)階者

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

前言

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

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

pip install pymongo from pymongo import MongoClient

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

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

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

2.用戶密碼登陸,安全等級(jí)高

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

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

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

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

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

這樣就創(chuàng)建了一student和teacher的集合了。然后我們?cè)賮盹@示一下所有的集合名:

show collections;
Python也能操作MongoDB數(shù)據(jù)庫

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

Python也能操作MongoDB數(shù)據(jù)庫

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

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

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

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

from pymongo import MongoClient
client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw')
db=client['hw']
db.create_collection('teacher')  創(chuàng)建集合
res={'id':'0001','name':'boy','age':36}
last=db.student.insert_one(res)  插入數(shù)據(jù) print(last.inserted_id) 打印id
Python也能操作MongoDB數(shù)據(jù)庫

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

import random
from pymongo import MongoClient
client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw')
db=client['hw']
coll=db['student'] def get(): for y in range(100000):
        data={'id':y,'name':'user--'+str(y),'age':random.choice(range(100))} yield data for y in get():
    coll.insert(y)
Python也能操作MongoDB數(shù)據(jù)庫

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

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

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

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

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

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

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

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

from pymongo import MongoClient
client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw')
db=client['hw']
coll=db['student']
coll.insert({'id':'111','name':'hw','age':43})
Python也能操作MongoDB數(shù)據(jù)庫

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

coll.update({'name':'hw'},{'$set':{'name':'用戶已注冊(cè)'}})
Python也能操作MongoDB數(shù)據(jù)庫

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

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

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

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

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

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

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

Python也能操作MongoDB數(shù)據(jù)庫

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

Python也能操作MongoDB數(shù)據(jù)庫

3.查找后刪除

Python也能操作MongoDB數(shù)據(jù)庫

4.查找后替換

Python也能操作MongoDB數(shù)據(jù)庫

5.查找后更新

Python也能操作MongoDB數(shù)據(jù)庫

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

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

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

coll.find().sort('name', pymongo.ASCENDING) # 升序排序 DESCENDING 降序排序
8.符合條件數(shù)量中跳過
coll.find().sort('name', pymongo.ASCENDING).skip(1) # 跳過一個(gè)記錄
9.限制符合條件輸出數(shù)量
coll.find().sort('name', pymongo.ASCENDING).limit(2) # 輸出兩個(gè)符合條件的記錄
10.通過Id來查找 

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

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

七、索引操作

1.創(chuàng)建索引

Python也能操作MongoDB數(shù)據(jù)庫

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

2.獲取索引

for y in coll.list_indexes(): # 獲取所有索引 print(y)
Python也能操作MongoDB數(shù)據(jù)庫

3.刪除索引

Python也能操作MongoDB數(shù)據(jù)庫

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

八、總結(jié)

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

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }