国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python常見MongoDB數(shù)據(jù)庫操作實例總結(jié)

2020-01-04 14:44:07
字體:
供稿:網(wǎng)友

本文實例講述了Python常見MongoDB數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:

MongoDB 是一個基于分布式文件存儲的數(shù)據(jù)庫。由C++語言編寫。旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲解決方案。

MongoDB 是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似json的bson格式,因此可以存儲比較復(fù)雜的數(shù)據(jù)類型。Mongo最大的特點是他支持的查詢語言非常強(qiáng)大,其語法有點類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能。接下來記錄一下在使用PyMongo操作MongoDB

下載pymongo庫

pip install pymongo

前置操作

# 獲取MongoDB操作,localhost為host,27017為MongoDB默認(rèn)portclient = 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ù)的idobject_id = res.inserted_idprint(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為一個listobject_ids = res.inserted_idsprint(object_ids)

查詢單條數(shù)據(jù)

# 查詢單條數(shù)據(jù),res為一個dictres = 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)只能進(jì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)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲解決方案。

MongoDB 是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似json的bson格式,因此可以存儲比較復(fù)雜的數(shù)據(jù)類型。Mongo最大的特點是他支持的查詢語言非常強(qiáng)大,其語法有點類似于面向?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)合使用.相當(dāng)于 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學(xué)習(xí)",只修改匹配到的第一條數(shù)據(jù)db.Student.update({name:"MongoDB"},{$set:{name:"MongoDB學(xué)習(xí)"}})# 找不到name="MongoDB"的數(shù)據(jù),則插入name="MongoDB學(xué)習(xí)",找到了則為修改。upsert:true找不到則插入,默認(rèn)false,不插入db.Student.update({name:"MongoDB"},{$set:{name:"MongoDB學(xué)習(xí)"}},{upsert:true})# 找到name="MongoDB"的數(shù)據(jù),將其更改為name="MongoDB學(xué)習(xí)"。multi:true更改所有匹配的數(shù)據(jù),默認(rèn)false,只匹配第一條db.Student.update({name:"MongoDB"},{$set:{name:"MongoDB學(xué)習(xí)"}},{multi:true})# 匹配name="MongoDB"的第一條數(shù)據(jù),將其更改為name="MongoDB學(xué)習(xí)"db.Student.updateOne({name:"MongoDB"},{$set:{name:"MongoDB學(xué)習(xí)"}})# 更新字典數(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字段值+1db.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為Stringdb.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=18db.Student.findAndModify({query:{name:"python"},update:{$set:{age:18}}})
  • ObjectId
# 獲取文檔的創(chuàng)建時間ObjectId("598542475e6b2464187abef7").getTimestamp()
  • aggregate:聚合查詢

常用聚合表達(dá)式:

 

表達(dá)式 描述
$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ù),默認(rèn)包含_id數(shù)據(jù)。讓不包含_id,可以使_id:0db.Student.aggregate({$project:{name:1,age:1}})

此時輸出的內(nèi)容只有_id,name,age,_id是默認(rèn)會輸出的,想不輸出_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管道操作符進(jìn)行處理

3. $skip:將前5個過濾掉

db.Student.aggregate({$skip:5})

$skip將前面5個數(shù)據(jù)過濾掉

希望本文所述對大家Python程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大理市| 满城县| 平度市| 漾濞| 独山县| 石门县| 井研县| 龙州县| 茂名市| 勃利县| 读书| 华坪县| 莫力| 格尔木市| 武宣县| 芦溪县| 开原市| 阿克陶县| 丰台区| 新郑市| 台州市| 桂林市| 宁津县| 堆龙德庆县| 新宁县| 平潭县| 和硕县| 阿克| 拉萨市| 鹤壁市| 达拉特旗| 武平县| 新竹县| 白河县| 襄汾县| 南丰县| 罗甸县| 梅河口市| 马山县| 东山县| 广德县|