mongodb MongoDB 彙總 group

來源:互聯網
上載者:User

標籤:

MongoDB 彙總

MongoDB中彙總(aggregate)主要用於處理資料(諸如統計平均值,求和等),並返回計算後的資料結果。有點類似sql語句中的 count(*)。

基本文法為:db.collection.aggregate( [ <stage1>, <stage2>, ... ] )

現在在mycol集合中有以下資料:

{ "_id" : 1, "name" : "tom", "sex" : "男", "score" : 100, "age" : 34 }
{ "_id" : 2, "name" : "jeke", "sex" : "男", "score" : 90, "age" : 24 }
{ "_id" : 3, "name" : "kite", "sex" : "女", "score" : 40, "age" : 36 }
{ "_id" : 4, "name" : "herry", "sex" : "男", "score" : 90, "age" : 56 }
{ "_id" : 5, "name" : "marry", "sex" : "女", "score" : 70, "age" : 18 }
{ "_id" : 6, "name" : "john", "sex" : "男", "score" : 100, "age" : 31 }

1、$sum 計算總和。

  Sql:  select sex,count(*) from mycol group by sex

  MongoDb:  db.mycol.aggregate([{$group: {_id: ‘$sex‘, personCount: {$sum: 1}}}])

  

  Sql:  select sex,sum(score) totalScore from mycol group by sex

  MongoDb:  db.mycol.aggregate([{$group: {_id: ‘$sex‘, totalScore: {$sum: ‘$score‘}}}])

  

2、$avg  計算平均值

   Sql:  select sex,avg(score) avgScore from mycol group by sex

  Mongodb: db.mycol.aggregate([{$group: {_id: ‘$sex‘, avgScore: {$avg: ‘$score‘}}}])

  

3、$max 擷取集合中所有文檔對應值得最大值。

  Sql:  select sex,max(score) maxScore from mycol group by sex

  Mongodb: db.mycol.aggregate([{$group: {_id: ‘$sex‘, maxScore : {$max: ‘$score‘}}}])

  

4、$min 擷取集合中所有文檔對應值得最小值。

  Sql:  select sex,min(score) minScore from mycol group by sex

  Mongodb: db.mycol.aggregate([{$group: {_id: ‘$sex‘, minScore : {$min: ‘$score‘}}}])

  

5、$push 把文檔中某一列對應的所有資料插入值到一個數組中。

  Mongodb: db.mycol.aggregate([{$group: {_id: ‘$sex‘, scores : {$push: ‘$score‘}}}])

  

6、$addToSet 把文檔中某一列對應的所有資料插入值到一個數組中,去掉重複的

  db.mycol.aggregate([{$group: {_id: ‘$sex‘, scores : {$addToSet: ‘$score‘}}}])

   

7、 $first 根據資來源文件的排序擷取第一個文檔資料。

   db.mycol.aggregate([{$group: {_id: ‘$sex‘, firstPerson : {$first: ‘$name‘}}}])

  

 

8、 $last 根據資來源文件的排序擷取最後一個文檔資料。

   db.mycol.aggregate([{$group: {_id: ‘$sex‘, lastPerson : {$last: ‘$name‘}}}])

  

管道的概念

管道在Unix和Linux中一般用於將當前命令的輸出結果作為下一個命令的參數。

MongoDB的彙總管道將MongoDB文檔在一個管道處理完畢後將結果傳遞給下一個管道處理。管道操作是可以重複的。

運算式:處理輸入文檔並輸出。運算式是無狀態的,只能用於計算當前彙總管道的文檔,不能處理其它的文檔。

這裡我們介紹一下彙總架構中常用的幾個操作:

  • $project:修改輸入文檔的結構。可以用來重新命名、增加或刪除域,也可以用於建立計算結果以及嵌套文檔。
  • $match:用於過濾資料,只輸出合格文檔。$match使用MongoDB的標準查詢操作。
  • $limit:用來限制MongoDB彙總管道返回的文檔數。
  • $skip:在彙總管道中跳過指定數量的文檔,並返回餘下的文檔。
  • $unwind:將文檔中的某一個數群組類型欄位拆分成多條,每條包含數組中的一個值。
  • $group:將集合中的文檔分組,可用於統計結果。
  • $sort:將輸入文檔排序後輸出。
  • $geoNear:輸出接近某一地理位置的有序文檔。

 1、$project執行個體

   db.mycol.aggregate({$project:{name : 1, score : 1}})

  

 

  這樣的話結果中就只還有_id,name和score三個欄位了,預設情況下_id欄位是被包含的,如果要想不包含_id話可以這樣:

   db.mycol.aggregate({$project:{_id : 0, name : 1, score : 1}})

  

 2、$match執行個體

  $match用於擷取分數大於30小於並且小於100的記錄,然後將合格記錄送到下一階段$group管道操作符進行處理

  db.mycol.aggregate([{$match :{score: {$gt: 30, $lt: 100}}},{$group:{_id:‘$sex‘,count:{$sum:1}}}]) 

  

 

mongodb MongoDB 彙總 group

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.