這篇文章給大家介紹Python中有哪些常見的MongoDB數(shù)據(jù)庫操作,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
和田ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!下載pymongo庫
pip install pymongo
前置操作
# 獲取MongoDB操作,localhost為host,27017為MongoDB默認port client = pymongo.MongoClient("mongodb://localhost:27017/") # 操作test數(shù)據(jù)庫 db = client.test # 獲取Student集合 student = db.Student
插入單條數(shù)據(jù)
# 插入一條數(shù)據(jù),并獲取返回結(jié)果 res = student.insert_one({"name":"老王"}) # 獲取插入之后該條數(shù)據(jù)的id object_id = res.inserted_id print(object_id)
插入多條數(shù)據(jù)
# 插入9條數(shù)據(jù) res = student.insert_many([{"name":"name%d"%index} for index in range(1,10)]) # 獲取插入之后該9條數(shù)據(jù)的ids,object_ids為一個list object_ids = res.inserted_ids print(object_ids)
查詢單條數(shù)據(jù)
# 查詢單條數(shù)據(jù),res為一個dict res = student.find_one({"name":"老王"})
查詢滿足條件的所有數(shù)據(jù)
# 查詢滿足條件的所有數(shù)據(jù),res為一個pymongo.cursor.Cursor對象 res = student.find({"name":"老王"}) # 獲取數(shù)據(jù)個數(shù) print(res.count()) for index in res: # index為一個dict。注意:這個循環(huán)只能進行一次,如需再次操作返回結(jié)果,需要在find一次,或?qū)ist(res),將這個返回結(jié)果保存起來 print(index)
更新
# 查詢并更新。{"name":"老王"}為查詢條件;{"$set":{"addr":"家住隔壁"}}更新數(shù)據(jù);upsert=False找不到不插入數(shù)據(jù),upsert=True找不到則插入數(shù)據(jù) # res為返回結(jié)果,res為一個字典對象,是之前數(shù)據(jù)的字典 res = student.find_one_and_update({"name":"老王"},{"$set":{"addr":"家住隔壁"}},upsert=False)
刪除單條數(shù)據(jù)
student.delete_one({"name":"老王"})
刪除匹配條件的所有數(shù)據(jù)
student.delete_many({"name":"老王"})
附:更多MongoDB的操作
MongoDB 是一個基于分布式文件存儲的數(shù)據(jù)庫。由C++語言編寫。旨在為WEB應(yīng)用提供可擴展的高性能數(shù)據(jù)存儲解決方案。
MongoDB 是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似json的bson格式,因此可以存儲比較復(fù)雜的數(shù)據(jù)類型。Mongo大的特點是他支持的查詢語言非常強大,其語法有點類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能。接下來記錄一下在終端怎么使用MongoDB:
常用命令
切換/創(chuàng)建數(shù)據(jù)庫
use xxx; # 切換數(shù)據(jù)庫,不存在則創(chuàng)建
插入數(shù)據(jù)
# 插入數(shù)據(jù),name="Python",age=100,Student為集合(表)名,Student不存在會自動創(chuàng)建 db.Student.insert({name:"Python",age:100})
或者定義一個字典
document = {name:"Python",age:100} db.Student.insert(document)
查詢數(shù)據(jù)
# 查詢所有數(shù)據(jù) db.Student.find() # 查詢所有數(shù)據(jù)并格式化輸出 db.Student.find().pretty() # 條件查詢,name="python"的所有數(shù)據(jù) db.Student.find({name:"python"}) # 條件查詢,age > 50的所有數(shù)據(jù) db.Student.find({age:{$gt:50}}) # 條件查詢,age >= 50的所有數(shù)據(jù) db.Student.find({age:{$gte:50}}) # 條件查詢,age < 50的所有數(shù)據(jù) db.Student.find({age:{$lt:50}}) # 條件查詢,age <= 50的所有數(shù)據(jù) db.Student.find({age:{$lte:50}}) # 條件查詢,age == 50的所有數(shù)據(jù) db.Student.find({age:{$eq:50}}) # 條件查詢,age != 50的所有數(shù)據(jù) db.Student.find({age:{$ne:50}}) # 條件查詢,存在name字段的所有數(shù)據(jù) db.Student.find({name:{$exists:true}}) # 多條件查詢,name="python"并且age=50的所有數(shù)據(jù) db.Student.find({name:"python",age:50}) # $and語法,name="python"并且age=50的所有數(shù)據(jù)。 db.Student.find({$and:[{name:"python"},{age:50}]}) # 查詢字典數(shù)組的數(shù)據(jù)infoList = [{"province":"廣東","city":"深圳"}] db.Student.find({"infoList.province":"廣東"}) # 查詢數(shù)量 db.Student.find({name:"python"}).count() # 或查詢,$or語法。查詢name="python"或name="android"的所有數(shù)據(jù) db.Student.find({$or:[{name:"python"},{name:"android"}]}) # $size語法,查詢info數(shù)組長度為8的所有數(shù)據(jù) db.Student.find({info:{$size:8}}) # $not語法,查詢info數(shù)組長度不為8的所有數(shù)據(jù) db.Student.find({info:{$not:{$size:8}}}) # and與or聯(lián)合使用.相當于 where age=18 and (name="python" or name="android") db.Student.find({age:18,$or:[{name:"python"},{name:"android"}]}) # $nor語法,搜索name既不等于"python"且不等于"android"的所有數(shù)據(jù) db.Student.find({"$nor":[{name:"python"},{name:"android"}]}) # $in語法.搜索name="老張"或name="老王"的所有數(shù)據(jù) db.Student.find({name:{$in:["老王","老張"]}}) # $nin語法.搜索name不為"老張"或"老王"的所有數(shù)據(jù) db.Student.find({name:{$nin:["老王","老張"]}}) # $all語法,搜索info=["aaa","bbb"]的所有數(shù)據(jù) db.Student.find({info:{$all:["aaa","bbb"]}}) # $mod語法,搜索sex % 2 == 0的所有數(shù)據(jù) db.Student.find({sex:{$mod:[2,0]}}) # $where語法,搜索age=info的所有數(shù)據(jù) db.Student.find({"$where":"this.age==this.info"}) # $slice語法,過濾,info數(shù)組中的后3個數(shù)據(jù) db.Student.find({},{info:{$slice:-3}}) # $slice語法,過濾,info數(shù)組中的前3個數(shù)據(jù) db.Student.find({},{info:{$slice:3}}) # $slice語法,過濾,info數(shù)組中跳過20個數(shù)據(jù)之后取10個數(shù)據(jù) db.Student.find({},{info:{$slice:[20,10]}}) # $slice語法,過濾,info數(shù)組中倒數(shù)第20個數(shù)據(jù)之后取10個數(shù)據(jù) db.Student.find({},{info:{$slice:[-20,10]}}) # 正則.獲取name包含"王"的所有數(shù)據(jù) db.Student.find({name:{$regex:"王"}}) # 正則。獲取name包含"a"并且不區(qū)分大小寫的所有數(shù)據(jù) db.Student.find({name:{$regex:"a",$options:"i"}})
更新數(shù)據(jù)
# 找到name="MongoDB"的數(shù)據(jù),將其更改為name="MongoDB學習",只修改匹配到的第一條數(shù)據(jù) db.Student.update({name:"MongoDB"},{$set:{name:"MongoDB學習"}}) # 找不到name="MongoDB"的數(shù)據(jù),則插入name="MongoDB學習",找到了則為修改。upsert:true找不到則插入,默認false,不插入 db.Student.update({name:"MongoDB"},{$set:{name:"MongoDB學習"}},{upsert:true}) # 找到name="MongoDB"的數(shù)據(jù),將其更改為name="MongoDB學習"。multi:true更改所有匹配的數(shù)據(jù),默認false,只匹配第一條 db.Student.update({name:"MongoDB"},{$set:{name:"MongoDB學習"}},{multi:true}) # 匹配name="MongoDB"的第一條數(shù)據(jù),將其更改為name="MongoDB學習" db.Student.updateOne({name:"MongoDB"},{$set:{name:"MongoDB學習"}}) # 更新字典數(shù)組的數(shù)據(jù)infoList = [{"province":"廣東","city":"深圳"}] db.Student.update({"infoList.province":"廣東"},{"$set":{"province.$.city":"廣州"}}) # 將age>18的數(shù)據(jù),修改name="xxx",第一個false:不存在不會插入(true為不存在則插入),第二個false:只匹配第一條數(shù)據(jù)(true為匹配所有數(shù)據(jù)) db.Student.update({age:{$gt:18}},{$set:{name:"xxx"}},false,false) # 在name="python"的所有數(shù)據(jù)里,將age字段值+1 db.Student.update({name:"python"},{$inc:{age:1}}) # 在name="python"的所有數(shù)據(jù)里,將age鍵刪除,1可以是任何值 db.Student.update({name:"python"},{$unset:{age:1}}) # 在name="python"的所有數(shù)據(jù)里,將age鍵名修改成"Age" db.Student.update({name:"python"},{$rename:{age:"Age"}}) # 在name="python"的所有數(shù)據(jù)里,在名為array的數(shù)組添加abc元素 db.Student.update({name:"python"},{$push:{array:"abc"}}) # 在name="python"的所有數(shù)據(jù)里,將["abc","adc"]里所有元素添加到array里面 db.Student.update({name:"python"},{$pushAll:{array:["abc","adc"]}}) # 在name="python"的所有數(shù)據(jù)里,在名為array的數(shù)組刪除abc元素 db.Student.update({name:"python"},{$pull:{array:"abc"}}) # 在name="python"的所有數(shù)據(jù)里,將["abc","adc"]里所有元素全部從array里刪除 db.Student.update({name:"python"},{$pullAll:{array:["abc","adc"]}}) # 在name="python"的所有數(shù)據(jù)里,刪除array數(shù)組尾部數(shù)據(jù),無論array為多少都只刪除一條,array小于0時,刪除頭部第一條,array大于等于0時,刪除尾部第一條 db.Student.update({name:"python"},{$pop:{array:2}})
刪除數(shù)據(jù)
# 刪除匹配到的所有數(shù)據(jù) db.Student.remove({name:"老張"}) # 刪除匹配到第一條數(shù)據(jù),justOne:true只刪除一條數(shù)據(jù) db.Student.remove({name:"老張"},{justOne:true})
**type**:type**:type操作符是基于BSON類型來檢索集合中匹配的數(shù)據(jù)類型,并返回結(jié)果
常用type類型:
數(shù)字 | 類型 |
---|---|
1 | Double |
2 | String |
3 | Object |
4 | Array |
5 | Binary data |
6 | Undefined |
7 | Object id |
8 | Boolean |
9 | Date |
10 | Null |
11 | Regular Expression |
13 | JavaScript |
14 | Symbol |
15 | JavaScript (with scope) |
16 | 32-bit integer |
17 | Timestamp |
18 | 64-bit integer |
255 | Min key |
127 | Max key |
# 查詢name為String類型的所有數(shù)據(jù),2為String db.Student.find({name:{$type:2}})
limit:限制條數(shù)
# 查詢name="python"的所有數(shù)據(jù),限制2條 db.Student.find({name:"python"}).limit(2)
skip:跳過數(shù)據(jù)
# 查詢name > 15的數(shù)據(jù),跳過前兩條,并限制只查詢兩條 db.Student.find({name:{$gt:15}}).limit(2).skip(2)
sort:排序,1位升序,-1位降序
# 查詢所有數(shù)據(jù),并以age升序排列 db.Student.find().sort({age:1}) # 多條件排序 db.Student.find().sort({age:1,score:-1})
findAndModify:查找并更新
# 查找name="python"的所有數(shù)據(jù),并修改age=18 db.Student.findAndModify({query:{name:"python"},update:{$set:{age:18}}})
ObjectId
# 獲取文檔的創(chuàng)建時間 ObjectId("598542475e6b2464187abef7").getTimestamp()
aggregate:聚合查詢
常用聚合表達式:
表達式 | 描述 |
---|---|
$sum | 和 |
$avg | 平均值 |
$min | 最小值 |
$max | 大值 |
$push | 在結(jié)果中插入值到數(shù)組中 |
$addToSet | 在結(jié)果中插入值到數(shù)組中,但不創(chuàng)建副本 |
$first | 根據(jù)資源文檔的排序,獲取第一個數(shù)據(jù) |
$last | 根據(jù)資源文檔的排序,獲取最后一個數(shù)據(jù) |
# 根據(jù)name分組,并插入sum,sum值為該組所有age的和 db.Student.aggregate([{$group:{_id:"$name",sum:{$sum:"$age"}}}]) # 根據(jù)name分組,并插入sum,sum值為該組的數(shù)量,并以sum排序,升序 db.Student.aggregate([{$group:{_id:"$name",sum:{$sum:1}}}]) # 根據(jù)name分組,并插入avg,avg值為該組所有age的平均值 db.Student.aggregate([{$group:{_id:"$name",avg:{$avg:"$age"}}}]) # 根據(jù)name分組,并插入min,min值為該組所有age的最小值 db.Student.aggregate([{$group:{_id:"$name",min:{$min:"$age"}}}]) # 根據(jù)name分組,并插入max,max值為該組所有age的大值 db.Student.aggregate([{$group:{_id:"$name",max:{$max:"$age"}}}]) # 根據(jù)name分組,并插入數(shù)組array,array值為該組所有的age值 db.Student.aggregate([{$group:{_id:"$name",array:{$push:"$age"}}}]) # 根據(jù)name分組,并插入數(shù)組array,array值為該組所有的age值 db.Student.aggregate([{$group:{_id:"$name",array:{$addToSet:"$age"}}}]) # 根據(jù)name分組,并插入f,f值為該組age下的第一個值 db.Student.aggregate([{$group:{_id:"$name",f:{$first:"$age"}}}]) # 根據(jù)name分組,并插入l,l值為該組age下的第一個值 db.Student.aggregate([{$group:{_id:"$name",l:{$last:"$age"}}}])
管道操作實例
1. $project:用于修改文檔的輸出結(jié)構(gòu)
# 查詢所有的name,age數(shù)據(jù),默認包含_id數(shù)據(jù)。讓不包含_id,可以使_id:0 db.Student.aggregate({$project:{name:1,age:1}})
此時輸出的內(nèi)容只有_id,name,age,_id是默認會輸出的,想不輸出_id,可以使_id:0
2. $match:用于過濾數(shù)據(jù)
db.Student.aggregate([{$match:{age:{$gt:19,$lte:23}}},{$group:{_id:null,count:{$sum:1}}}])
match過濾出age大于19且小于等于23的數(shù)據(jù),然后將符合條件的記錄送到下一階段match過濾出age大于19且小于等于23的數(shù)據(jù),然后將符合條件的記錄送到下一階段group管道操作符進行處理
3. $skip:將前5個過濾掉
db.Student.aggregate({$skip:5})
$skip將前面5個數(shù)據(jù)過濾掉
關(guān)于Python中有哪些常見的MongoDB數(shù)據(jù)庫操作就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
文章標題:Python中有哪些常見的MongoDB數(shù)據(jù)庫操作-創(chuàng)新互聯(lián)
文章位置:http://aaarwkj.com/article14/ddooge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、建站公司、品牌網(wǎng)站設(shè)計、定制網(wǎng)站、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容