學習MongoDB 九: MongoDB聚合(單一用途的聚合方法)(一)

           聚合指各種可以處理批量記錄並返回計算結果的操作,並MongoDB提供了豐富的聚合操作,MongoDB提供了進行聚合的三種方式:聚集管道(Aggregation),Map-Reduce方法,和單一用途的聚合方法。

       單一用途的聚合方法:db.collection.count(), db.collection.group(), db.collection.distinct()。


      一:db.collection.count()  返回匹配查詢結果的數量

          db.collection.count(query, options)  返回匹配查詢結果的數量  相當於mysql的語法:select count(1) from orders where 條件。

          

> db.orders.find()
{ "_id" : ObjectId("57383f492bd2092c7ed0fec7"), "ino" : "001", "quantity" : 2, "
price" : 4 }
{ "_id" : ObjectId("57383f492bd2092c7ed0fec8"), "ino" : "002", "quantity" : 2, "
price" : 6 }
{ "_id" : ObjectId("57383f492bd2092c7ed0fec9"), "ino" : "003", "quantity" : 3, "
price" : 5 }
> db.orders.count()
3

> db.orders.count({quantity:{$gt:2}})
1
 

      也可以這樣獲取返回匹配查詢結果的數量:

      var cursor=db.items.find()  可以以查詢只包含索引鍵的條件,  cursor.count()。

   二: db.collection.distinct()  返回某個字段的非重複值列表

       db.collection.distinct(field, query)  返回指定某個字段的非重複的值的列表,結果不能大於最大BSON大小(大小爲4 MB)    相當於mysql的語法:select distinct(field) from orders where 條件。

> db.orders.find()
{ "_id" : ObjectId("57383f492bd2092c7ed0fec7"), "ino" : "001", "quantity" : 2, "
price" : 4 }
{ "_id" : ObjectId("57383f492bd2092c7ed0fec8"), "ino" : "002", "quantity" : 2, "
price" : 6 }
{ "_id" : ObjectId("57383f492bd2092c7ed0fec9"), "ino" : "003", "quantity" : 3, "
price" : 5 }
> db.orders.distinct("quantity")
[ 2, 3 ]

    三: db.collection.group()  

      db.collection.group({ key, reduce, initial[, keyf] [, cond] [, finalize] })

      我們比較熟悉的group by 的sql語句select key from table  group by key,而mongoDB沒提供SQL那樣通過group By就輕鬆實現數據庫的分組功能, db.collection.group()是對某個字段的對集合進行分組,然後通過聚合每一組中的所有文檔,可以對聚合每一組中的所有文檔進行處理,來產生最終的我們想要的結果文檔。
       db.collection.group()使用JavaScript,它受到了一些性能上的限制。大多數情況下,$ group在Aggregation Pipeline提供了一種具有較少的限制適用的替代。可以通過指定的鍵的集合中的文檔和執行簡單的聚合函數。
         (1)在2.2版本中,返回的數組可以包含最多20000個元素;即最多20000個獨特的分組。
         (2)聚集管道(Aggregation),Map-Reduce方法都可以運行在分片集合,group()方法不能運行在分片集羣中工作。 
         (3)結果集必須符合最大BSON文檔大小(大小爲4 MB)。
 
 1. 訂單集合記錄日期和明細中的數量、產品編碼,我們訂單集合按照對日期和產品編碼進行分組字段,然後對每一組文檔進行處理,找出並計算相同的產品的數量。Sql語句:Select pnumber,sum(quantity) as total from orders,items group by pnumber(少了兩張表的關聯的條件)。
      
> db.orders.find()
{ "_id" : ObjectId("573848342bd2092c7ed0feca"), "onumber" : "001", "date" : ISOD
ate("2014-01-02T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 2, "price"
 : 5, "pnumber" : "p003" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecb"), "onumber" : "002", "date" : ISOD
ate("2014-01-03T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 1, "price"
 : 4, "pnumber" : "p002" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecc"), "onumber" : "003", "date" : ISOD
ate("2014-01-04T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 10, "price
" : 2, "pnumber" : "p001" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecd"), "onumber" : "003", "date" : ISOD
ate("2014-01-04T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 30, "price
" : 4, "pnumber" : "p002" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fece"), "onumber" : "004", "date" : ISOD
ate("2014-01-05T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 5, "price"
 : 4, "pnumber" : "p002" } }
 
>  db.orders.group({
...           key: {'item.pnumber':1},
...           initial : {"total":0},
...          reduce : function Reduce(doc, out) {
...         out.total+=doc.item.quantity
...    } });
[
        {
                "item.pnumber" : "p003",
                "total" : 2
        },
        {
                "item.pnumber" : "p002",
                "total" : 36
        },
        {
                "item.pnumber" : "p001",
                "total" : 10
        }
]

   描述:
     key:作爲分組的key。
     reduce:一個聚合函數操作文檔的分組操作期間。這些函數可以返回一個sum或count。該函數接受兩個參數:當前  文檔和這個羣體聚集的結果文檔。
     initial:初始化聚合結果文檔變量,爲空時自動爲每列提供初始變量。
     keyf:可選。替代的key 字段。指定一個函數創建一個“key object”作爲分組的key。使用keyf而是通過group by領域而不是現有的文檔域鍵組。
     cond:過濾條件,根據條件過濾集合的文檔。
  
      2、完成器【finalize】
       
          我們對finalize方法 進行詳細的介紹,在db.collection.group()返回最終結果之前,每一組文檔執行完後,多會觸發此方法,此功能可以修改的結果文檔或替換的結果文檔作爲一個整體,執行group()結果集必須符合最大BSON文檔大小(大小爲4 MB),finalize能對數據傳到客戶時,進行裁剪結果,可以提高很大的效率。
  
       (1) 我們對訂單集合根據日期進行分組,並對相同的產品號數量進行累加,對累加完的產品數量沒有大於20 的我們進行刪除。減少返回的數據。
            
         
  db.orders.group({
           key: {date:1},
           initial : {"pnumbers":{}},
           reduce : function Reduce(doc, out) {
		 if(out.pnumbers[doc.item.pnumber]==null){
		    out.pnumbers[doc.item.pnumber]=new Object();
		    out.pnumbers[doc.item.pnumber]=doc.item.quantity;
		 }else{
		   out.pnumbers[doc.item.pnumber]+=doc.item.quantity;
		} 
          },finalize : function Finalize(doc) {
      	     for(i in doc.pnumbers)
            {
              if (doc.pnumbers[i] < 20)
               {
                   delete doc.pnumbers[i];
               }
             }
    } });
  
  [
        {
                "date" : ISODate("2014-01-02T16:03:00Z"),
                "pnumbers" : {

                }
        },
        {
                "date" : ISODate("2014-01-03T16:03:00Z"),
                "pnumbers" : {

                }
        },
        {
                "date" : ISODate("2014-01-04T16:03:00Z"),
                "pnumbers" : {
                        "p002" : 30
                }
        },
        {
                "date" : ISODate("2014-01-05T16:03:00Z"),
                "pnumbers" : {

                }
        }
]

    (2)每一組文檔執行完後,多會觸發此方法,此功能可以修改的結果文檔,我們對訂單的集合實現一天賣出了多少個產品,金額是多少,平均價格是多少。
     
     
    db.orders.group({
       key: {date:1},
       initial :{"total":0,"money":0},
       reduce : function Reduce(doc, out) {
       out.total+=doc.item.quantity;
          out.money+=doc.item.quantity*doc.item.price;
        },
       finalize : function Finalize(out) {
         out.avg=out.money/out.total
         return out;
     }
  });
  [
        {
                "date" : ISODate("2014-01-02T16:03:00Z"),
                "total" : 2,
                "money" : 10,
                "avg" : 5
        },
        {
                "date" : ISODate("2014-01-03T16:03:00Z"),
                "total" : 1,
                "money" : 4,
                "avg" : 4
        },
        {
                "date" : ISODate("2014-01-04T16:03:00Z"),
                "total" : 40,
                "money" : 140,
                "avg" : 3.5
        },
        {
                "date" : ISODate("2014-01-05T16:03:00Z"),
                "total" : 5,
                "money" : 20,
                "avg" : 4
        }
] 

   3.keyf的使用
       可以接受一個javascript函數,用來動態的確定分組文檔的字段,和key兩者必須有一個。我們有時用到比較複雜的key時,可以通過keyf的方法使用javascript函數對要進行分組的字段先進行特殊的處理,然後在做爲key進行分組。

     我們對訂單集合,按照日期的月份進行分組,我們保存的文檔的日期是到天,所以我們先轉換爲月,並計算月份賣出了多少個產品,金額是多少,平均價格是多少。


	 
> db.orders.find({})
{ "_id" : ObjectId("573848342bd2092c7ed0feca"), "onumber" : "001", "date" : ISOD
ate("2014-01-02T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 2, "price"
 : 5, "pnumber" : "p003" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecb"), "onumber" : "002", "date" : ISOD
ate("2014-01-03T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 1, "price"
 : 4, "pnumber" : "p002" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecc"), "onumber" : "003", "date" : ISOD
ate("2014-01-04T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 10, "price
" : 2, "pnumber" : "p001" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecd"), "onumber" : "003", "date" : ISOD
ate("2014-01-04T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 30, "price
" : 4, "pnumber" : "p002" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fece"), "onumber" : "004", "date" : ISOD
ate("2014-01-05T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 5, "price"
 : 4, "pnumber" : "p002" } }
>  db.orders.group({
...    keyf: function (doc){
...    return{'month':doc.date.getMonth()+1};
...  },
...  initial :{"total":0,"money":0},
...  reduce : function Reduce(doc, out) {
...     out.total+=doc.item.quantity;
...           out.money+=doc.item.quantity*doc.item.price;
...
... },
...  finalize : function Finalize(out) {
...          out.avg=out.money/out.total
...          return out;
...   }
... });
[ { "month" : 1, "total" : 48, "money" : 174, "avg" : 3.625 } ]
	 


       MongoDB提供了進行聚合的三種方式:聚集管道(Aggregation),Map-Reduce方法,和單一用途的聚合方法,先介紹了單一用途的聚合方法的使用方法,接下去介紹聚集管道(Aggregation)和Map-Reduce方法。
    



   
         




發佈了69 篇原創文章 · 獲贊 112 · 訪問量 100萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章