1. 程式人生 > >mongodb group操作

mongodb group操作

因為應用訪問日誌由mongodb來儲存,但一起沒有做統計,學習一下group分類的相關查詢。

需求

檢視指定appId在某一個時間段內的訪問次數

db.getCollection('Collection').group(
{
  key:{ "cardDataId":true},
  initial: {count:0},
  reduce:function(obj,prev){prev.count++;},
  condition:{
     "appId":"2de76bfb26af4b9a844e2ac6597b0c50",
      $and:[{insertDate:{$gte
:'2017-05-01 00:00:00'}},{insertDate:{$lte:'2017-05-08 00:00:00'}}] } })

總結group引數選項:

1.key: 這個就是分組的key
2.initial: 每組都分享一個初始化函式,特別注意:是每一組initial函式。
3.reduce: 這個函式的第一個引數是當前的文件物件,第二個引數是上一次function操作的累計物件。有多少個文件, $reduce就會呼叫多少次。
4.condition: 這個就是過濾條件。
5.finalize: 這是個函式,每一組文件執行完後,多會觸發此方法。

group的其它使用方法例項

準備測試資料

db.user.drop();
for(var i=10; i< 100; i++) {
  db.user.insert({
    name:"user" + i, 
    age : Math.floor(Math.random()*10)+ 20, 
    sex : Math.floor(Math.random()*3)%2 ==0 ? 'M' : 'F',
    chinese : Math.floor(Math.random()*50)+50,
    math : Math.floor(Math.random()*50)+50,
    english : Math.floor(Math.random
()*50)+50, class : "C" + i%5 }) }

group函式按照class進行分組,顯示每個class中的使用者姓名和性別

db.user.group({
  key: {"class": true},
  initial: {"person": []},
  reduce: function(cur, prev) {
    prev.person.push({name: cur.name, sex: cur.sex, age: cur.age});
  }
});

對age>25的使用者,按照class進行分組,顯示每個class中的使用者姓名和性別,並統計每組的人數

db.user.group({
  key: {"class": true},
  initial: {"person": []},
  reduce: function(doc, out){
    out.person.push({name: doc.name, sex: doc.sex, age: doc.age});
  },
  finalize: function(out){
    out.count = out.person.length;
  },
  condition: {"age": {$gt: 25}}
})

分組計算每個class中,chinese最大值和最小值

db.user.group({
  key: {"class": true},
  initial: {"chinese_min": 0, "chinese_max":0 },
  reduce: function(doc, out){
    out.chinese_min = doc.chinese;
    out.chinese_min = doc.chinese;
    out.chinese_min = Math.min(out.chinese_min, doc.chinese);
    out.chinese_max = Math.max(out.chinese_max, doc.chinese)
  },
})

利用分組,計算每個總成績和成績平均值

db.user.group({
  key: {"_id" : true},
  initial: {name:"", total: 0, avg: 0},
  reduce: function(doc, out){
    out.name = doc.name;
    out.total = doc.chinese + doc.math + doc.english;
    out.avg = Math.floor(out.total / 3);
  }
})