Mongodb官方網(wǎng)站提供了一個美國人口統(tǒng)計(jì)數(shù)據(jù),下載地址如下
在塔河等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需搭建網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),營銷型網(wǎng)站,外貿(mào)營銷網(wǎng)站建設(shè),塔河網(wǎng)站建設(shè)費(fèi)用合理。http://media.mongodb.org/zips.json
數(shù)據(jù)示例:
[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將數(shù)據(jù)導(dǎo)入mongodb數(shù)據(jù)庫
[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等簡單操作
實(shí)例1.1:求zipcodes集合的文檔數(shù)
db.zipcodes.count()實(shí)例1.2 求MA州的文檔總數(shù)
db.zipcodes.count({state:"MA"})實(shí)例1.3 求zipcodes中有哪些州
db.zipcodes.distinct("state")二、使用aggregate聚合框架,進(jìn)行更復(fù)雜的聚合操作
實(shí)例2.1:統(tǒng)計(jì)每個州的人口總數(shù)
db.zipcodes.aggregate( [ { $group: { _id: "$state", total: { $sum: "$pop" } } } ] )使用集合的aggregate方法,進(jìn)行聚合查詢。
$group關(guān)鍵字后面指定分組的字段(引用字段時(shí),一定要用$前綴),以及聚合函數(shù)。
_id:是關(guān)鍵字,代表返回結(jié)果集的主鍵。
該查詢等價(jià)的SQL為
select state as _id,sum(pop) as total from zipcodes group by state實(shí)例2.2:統(tǒng)計(jì)每個州每個城市的人口總數(shù)
db.zipcodes.aggregate( [ { $group: { _id: {state:"$state",city:"$city"}, pop: { $sum: "$pop" } } }, ] )分組的字段如果多于一個,那么每個字段都要給定一個別名,如 state:"$state"
實(shí)例2.3:統(tǒng)計(jì)每個州人口多于10000的城市的人口總和
db.zipcodes.aggregate( [ { $match: {"pop":{$gt: 10000} }}, { $group: { _id: {state:"$state"}, pop: { $sum: "$pop" } } }, ] )$match 關(guān)鍵字后面跟上集合的過濾條件 。該語句等價(jià)于如下SQL
select state,sum(pop) as pop from zipcodes where pop>10000 group by state實(shí)例2.4:查詢?nèi)丝诳倲?shù)超過1千萬的州
db.zipcodes.aggregate( [ { $group: { _id: {state:"$state"}, pop: { $sum: "$pop" } } }, { $match: {"pop":{$gt: 1000*10000} }} ] )將$match放在$group后面,相當(dāng)于是先執(zhí)行g(shù)roup操作,再對結(jié)果集進(jìn)行過濾。等價(jià)的sql如下
select state,sum(pop) as pop from zipcodes group by state having sum(pop)>1000*10000實(shí)例5:求每個州城市的平均人口
db.zipcodes.aggregate( [ { $group: { _id: {state:"$state",city:"$city"}, pop: { $sum: "$pop" } } }, { $group: {_id:"$_id.state",avgPop:{$avg: "$pop"}}} ] )我們的aggregate函數(shù)支持多次迭代,該語句的等價(jià)sql為
select state,avg(pop) as avgPop from (select state,city,sum(pop) pop from zipcodes group by state,city) group by state實(shí)例2.5 :求每個州人口最多及最少的城市名及對應(yīng)的人口數(shù)量
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分組的人口數(shù)。
$sort操作按照人口數(shù)排序
第二個$group 按照state分組,此時(shí)每個state分組的數(shù)據(jù)已經(jīng)安裝cityPop排序。每個組的第一行數(shù)據(jù)($first 取得)是人口最少的city,最后一行($last 取得)是人口最多的city。
實(shí)例2.6 利用$project重新格式化結(jié)果
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" } } } ] )實(shí)例2.7 對數(shù)組中的內(nèi)容做聚合統(tǒng)計(jì)
我們假設(shè)有一個學(xué)生選課的集合,數(shù)據(jù)示例如下
db.course.insert({name:"張三",age:10,grade:"四年級",course:["數(shù)學(xué)","英語","政治"]}) db.course.insert({name:"李四",age:9,grade:"三年級",course:["數(shù)學(xué)","語文","自然"]}) db.course.insert({name:"王五",age:11,grade:"四年級",course:["數(shù)學(xué)","英語","語文"]}) db.course.insert({name:"趙六",age:9,grade:"四年級",course:["數(shù)學(xué)","歷史","政治"]})求每門課程有多少人選修
db.course.aggregate( [ { $unwind: "$course" }, { $group: { _id: "$course", sum: { $sum: 1 } } }, { $sort: { sum: -1 } } ] )$unwind,用來將數(shù)組中的內(nèi)容拆包,然后再按照拆包后的數(shù)據(jù)進(jìn)行分組,另外aggregate中沒有$count關(guān)鍵字,使用$sum:1 來計(jì)算count 。
實(shí)例2.8 求每個州有哪些city。
db.zipcodes.aggregate( [ { $group: { _id: "$state", cities: { $addToSet: "$city"} } }, ] )$addToSet 將每個分組的city內(nèi)容,寫到一個數(shù)組中。
假設(shè)我們有如下數(shù)據(jù)結(jié)構(gòu)
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" ] } } ] })該文檔描述書的章節(jié)內(nèi)容,每章節(jié)有tags字段,書本身也有tags字段。
如果客戶有需要,查詢帶有標(biāo)簽Mongodb的書,以及只顯示有標(biāo)簽Mongodb的章節(jié)。我們使用find()方法是無法滿足的。
db.book.find( { $or: [{tags:{$in: ['Mongodb']}}, {"subsections.tags":{$in: ['Mongodb']}} ] } )上面類似的查詢,會顯示命中文檔的所有部分,把不包含Mongodb標(biāo)簽的章節(jié)也顯示出來了。
Aggregate提供了一個$redact表達(dá)式,可以對結(jié)果進(jìn)行裁剪。
db.book.aggregate( [ {$redact: { $cond: { if: { $gt:[ {$size: {$setIntersection: ["$tags",["Mongodb"]] }},0] }, then:"$$DESCEND" , else: "$$PRUNE" } }} ] )$$DESCEND 如果滿足條件,則返回條件tags字段,對于內(nèi)嵌文檔,則返回父級字段。所有判斷條件會作用到內(nèi)嵌文檔中。
$$PRUNE 如果不滿足條件,則不顯示該字段。
查詢結(jié)果如下
{ "_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
實(shí)例3.1 :統(tǒng)計(jì)每個州的人口總數(shù)
db.zipcodes.mapReduce( function () {emit(this.state, this.pop)}, //mapFunction (key, values)=>{return Array.sum(values)},//reduceFunction { out: "zipcodes_groupby_state"} )使用mapReduce,最少有三個參數(shù),map函數(shù)、reduce函數(shù)、out輸出參數(shù)。
map函數(shù)中,this表示處理的當(dāng)前文檔。emit函數(shù),將傳入的鍵值對傳出給reduce函數(shù)。
reduce接受map函數(shù)的輸出,作為輸入。reduce中的values是一個列表。對上例來說,state是鍵,相同state的每條記錄對應(yīng)的pop組成一個列表作為值。形式如下
state = "CA" values=[51841,40629,...]
reduce函數(shù)的key是默認(rèn)一定會返回的,return的返回值,將values中的值相加。作為值。
out:輸出結(jié)果保存的集合
實(shí)例3.2 統(tǒng)計(jì)每個城市的人口數(shù),及每個城市的文檔個數(shù)。
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}作為一個對象當(dāng)成值,傳遞給map函數(shù)的key。將{count:1,pop:this.pop}對象傳遞給map的value 。
再reduce函數(shù)中再次計(jì)算count,pop的值。返回。
等價(jià)的sql如下
select state,city,count(*) as count,sum(pop) as pop from zipcodes group by state,city另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。