您好,登錄后才能下訂單哦!
Mongodb官方網站提供了一個美國人口統計數據,下載地址如下
http://media.mongodb.org/zips.json
數據示例:
[root@localhost cluster]# head zips.json { "_id" : "01001", "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA" } { "_id" : "01002", "city" : "CUSHMAN", "loc" : [ -72.51564999999999, 42.377017 ], "pop" : 36963, "state" : "MA" } { "_id" : "01005", "city" : "BARRE", "loc" : [ -72.10835400000001, 42.409698 ], "pop" : 4546, "state" : "MA" } { "_id" : "01007", "city" : "BELCHERTOWN", "loc" : [ -72.41095300000001, 42.275103 ], "pop" : 10579, "state" : "MA" } { "_id" : "01008", "city" : "BLANDFORD", "loc" : [ -72.936114, 42.182949 ], "pop" : 1240, "state" : "MA" } { "_id" : "01010", "city" : "BRIMFIELD", "loc" : [ -72.188455, 42.116543 ], "pop" : 3706, "state" : "MA" } { "_id" : "01011", "city" : "CHESTER", "loc" : [ -72.988761, 42.279421 ], "pop" : 1688, "state" : "MA" } { "_id" : "01012", "city" : "CHESTERFIELD", "loc" : [ -72.833309, 42.38167 ], "pop" : 177, "state" : "MA" } { "_id" : "01013", "city" : "CHICOPEE", "loc" : [ -72.607962, 42.162046 ], "pop" : 23396, "state" : "MA" } { "_id" : "01020", "city" : "CHICOPEE", "loc" : [ -72.576142, 42.176443 ], "pop" : 31495, "state" : "MA" }
使用mongoimport將數據導入mongodb數據庫
[root@localhost cluster]# mongoimport -d test -c "zipcodes" --file zips.json -h 192.168.199.219:27020 2016-01-16T18:31:29.424+0800 connected to: 192.168.199.219:27020 2016-01-16T18:31:32.420+0800 [################........] test.zipcodes 2.1 MB/3.0 MB (68.5%) 2016-01-16T18:31:34.471+0800 [########################] test.zipcodes 3.0 MB/3.0 MB (100.0%) 2016-01-16T18:31:34.471+0800 imported 29353 documents
一、單一目的的聚合操作
求count,distinct等簡單操作
實例1.1:求zipcodes集合的文檔數
db.zipcodes.count()
實例1.2 求MA州的文檔總數
db.zipcodes.count({state:"MA"})
實例1.3 求zipcodes中有哪些州
db.zipcodes.distinct("state")
二、使用aggregate聚合框架,進行更復雜的聚合操作
實例2.1:統計每個州的人口總數
db.zipcodes.aggregate( [ { $group: { _id: "$state", total: { $sum: "$pop" } } } ] )
使用集合的aggregate方法,進行聚合查詢。
$group關鍵字后面指定分組的字段(引用字段時,一定要用$前綴),以及聚合函數。
_id:是關鍵字,代表返回結果集的主鍵。
該查詢等價的SQL為
select state as _id,sum(pop) as total from zipcodes group by state
實例2.2:統計每個州每個城市的人口總數
db.zipcodes.aggregate( [ { $group: { _id: {state:"$state",city:"$city"}, pop: { $sum: "$pop" } } }, ] )
分組的字段如果多于一個,那么每個字段都要給定一個別名,如 state:"$state"
實例2.3:統計每個州人口多于10000的城市的人口總和
db.zipcodes.aggregate( [ { $match: {"pop":{$gt: 10000} }}, { $group: { _id: {state:"$state"}, pop: { $sum: "$pop" } } }, ] )
$match 關鍵字后面跟上集合的過濾條件 。該語句等價于如下SQL
select state,sum(pop) as pop from zipcodes where pop>10000 group by state
實例2.4:查詢人口總數超過1千萬的州
db.zipcodes.aggregate( [ { $group: { _id: {state:"$state"}, pop: { $sum: "$pop" } } }, { $match: {"pop":{$gt: 1000*10000} }} ] )
將$match放在$group后面,相當于是先執行group操作,再對結果集進行過濾。等價的sql如下
select state,sum(pop) as pop from zipcodes group by state having sum(pop)>1000*10000
實例5:求每個州城市的平均人口
db.zipcodes.aggregate( [ { $group: { _id: {state:"$state",city:"$city"}, pop: { $sum: "$pop" } } }, { $group: {_id:"$_id.state",avgPop:{$avg: "$pop"}}} ] )
我們的aggregate函數支持多次迭代,該語句的等價sql為
select state,avg(pop) as avgPop from (select state,city,sum(pop) pop from zipcodes group by state,city) group by state
實例2.5 :求每個州人口最多及最少的城市名及對應的人口數量
db.zipcodes.aggregate( [ { $group: { _id: {state:"$state",city:"$city"}, cityPop: { $sum: "$pop" } } }, { $sort: { cityPop: 1 } }, { $group: { _id:"$_id.state", biggestCity:{$last:"$_id.city"}, biggestPop:{$last:"$cityPop"}, smallestCity:{$first:"$_id.city"}, smallestPop:{$first:"$cityPop"} }} ] )
第一個$group求出按state,city分組的人口數。
$sort操作按照人口數排序
第二個$group 按照state分組,此時每個state分組的數據已經安裝cityPop排序。每個組的第一行數據($first 取得)是人口最少的city,最后一行($last 取得)是人口最多的city。
實例2.6 利用$project重新格式化結果
db.zipcodes.aggregate( [ { $group: { _id: {state:"$state",city:"$city"}, cityPop: { $sum: "$pop" } } }, { $sort: { cityPop: 1 } }, { $group: { _id:"$_id.state", biggestCity:{$last:"$_id.city"}, biggestPop:{$last:"$cityPop"}, smallestCity:{$first:"$_id.city"}, smallestPop:{$first:"$cityPop"} } }, { $project: { _id:0, state: "$_id", biggestCity: { name: "$biggestCity", pop: "$biggestPop" }, smallestCity: { name: "$smallestCity", pop: "$smallestPop" } } } ] )
實例2.7 對數組中的內容做聚合統計
我們假設有一個學生選課的集合,數據示例如下
db.course.insert({name:"張三",age:10,grade:"四年級",course:["數學","英語","政治"]}) db.course.insert({name:"李四",age:9,grade:"三年級",course:["數學","語文","自然"]}) db.course.insert({name:"王五",age:11,grade:"四年級",course:["數學","英語","語文"]}) db.course.insert({name:"趙六",age:9,grade:"四年級",course:["數學","歷史","政治"]})
求每門課程有多少人選修
db.course.aggregate( [ { $unwind: "$course" }, { $group: { _id: "$course", sum: { $sum: 1 } } }, { $sort: { sum: -1 } } ] )
$unwind,用來將數組中的內容拆包,然后再按照拆包后的數據進行分組,另外aggregate中沒有$count關鍵字,使用$sum:1 來計算count 。
實例2.8 求每個州有哪些city。
db.zipcodes.aggregate( [ { $group: { _id: "$state", cities: { $addToSet: "$city"} } }, ] )
$addToSet 將每個分組的city內容,寫到一個數組中。
假設我們有如下數據結構
db.book.insert({ _id: 1, title: "MongoDB Documentation", tags: [ "Mongodb", "NoSQL" ], year: 2014, subsections: [ { subtitle: "Section 1: Install MongoDB", tags: [ "NoSQL", "Document" ], content: "Section 1: This is the content of section 1." }, { subtitle: "Section 2: MongoDB CRUD Operations", tags: [ "Insert","Mongodb" ], content: "Section 2: This is the content of section 2." }, { subtitle: "Section 3: Aggregation", tags: [ "Aggregate" ], content: { text: "Section 3: This is the content of section3.", tags: [ "MapReduce","Aggregate" ] } } ] })
該文檔描述書的章節內容,每章節有tags字段,書本身也有tags字段。
如果客戶有需要,查詢帶有標簽Mongodb的書,以及只顯示有標簽Mongodb的章節。我們使用find()方法是無法滿足的。
db.book.find( { $or: [{tags:{$in: ['Mongodb']}}, {"subsections.tags":{$in: ['Mongodb']}} ] } )
上面類似的查詢,會顯示命中文檔的所有部分,把不包含Mongodb標簽的章節也顯示出來了。
Aggregate提供了一個$redact表達式,可以對結果進行裁剪。
db.book.aggregate( [ {$redact: { $cond: { if: { $gt:[ {$size: {$setIntersection: ["$tags",["Mongodb"]] }},0] }, then:"$$DESCEND" , else: "$$PRUNE" } }} ] )
$$DESCEND 如果滿足條件,則返回條件tags字段,對于內嵌文檔,則返回父級字段。所有判斷條件會作用到內嵌文檔中。
$$PRUNE 如果不滿足條件,則不顯示該字段。
查詢結果如下
{ "_id" : 1, "title" : "MongoDB Documentation", "tags" : [ "Mongodb", "NoSQL" ], "year" : 2014, "subsections" : [ { "subtitle" : "Section 2: MongoDB CRUD Operations", "tags" : [ "Insert", "Mongodb" ], "content" : "Section 2: This is the content of section 2." } ] }
三、使用mapReduce
實例3.1 :統計每個州的人口總數
db.zipcodes.mapReduce( function () {emit(this.state, this.pop)}, //mapFunction (key, values)=>{return Array.sum(values)},//reduceFunction { out: "zipcodes_groupby_state"} )
使用mapReduce,最少有三個參數,map函數、reduce函數、out輸出參數。
map函數中,this表示處理的當前文檔。emit函數,將傳入的鍵值對傳出給reduce函數。
reduce接受map函數的輸出,作為輸入。reduce中的values是一個列表。對上例來說,state是鍵,相同state的每條記錄對應的pop組成一個列表作為值。形式如下
state = "CA" values=[51841,40629,...]
reduce函數的key是默認一定會返回的,return的返回值,將values中的值相加。作為值。
out:輸出結果保存的集合
實例3.2 統計每個城市的人口數,及每個城市的文檔個數。
db.zipcodes.mapReduce( function () { var key = {state:this.state,city:this.city} emit(key, {count:1,pop:this.pop}) }, //mapFunction (key, values)=>{ var retval = {count:0,pop:0} for (var i =0;i< values.length;i++){ retval.count += values[i].count retval.pop += values[i].pop } return retval },//reduceFunction { out: "zipcodes_groupby_state_city"} )
我們將{state,city}作為一個對象當成值,傳遞給map函數的key。將{count:1,pop:this.pop}對象傳遞給map的value 。
再reduce函數中再次計算count,pop的值。返回。
等價的sql如下
select state,city,count(*) as count,sum(pop) as pop from zipcodes group by state,city
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。