標籤:order src group select term get 添加 _id not
原文:http://blog.csdn.net/congcong68/article/details/45012717
一.簡介
db.collection.group()使用JavaScript,它受到了一些效能上的限制。大多數情況下,$ group在Aggregation Pipeline提供了一種具有較少的限制適用的替代。可以通過指定的鍵的集合中的文檔和執行簡單的彙總函式。在2.2版本中,返回的數組可以包含最多20000個元素;即最多20000個獨特的分組。
我們比較熟悉的group by 的sql語句select key from table groupby key,而mongoDB沒提供SQL那樣通過Group By就輕鬆實現資料庫的分組功能,我們通過介面來實現的
db.collection.group({ key, reduce, initial[, keyf] [, cond] [, finalize] })
key |
作為分組的key |
reduce |
一個彙總函式操作文檔的分組操作期間。這些函數可以返回一個sum或count。該函數接受兩個參數:當前文檔和這個群體聚集的結果文檔。 |
initial |
初始化彙總結果文檔變數,為空白時自動為每列提供初始變數。 |
keyf |
可選。替代的key 欄位。指定一個函數建立一個“key object”作為分組的key。使用keyf而是通過group by領域而不是現有的文檔域鍵組。 |
cond |
過濾條件 |
finalize |
在db.collection.group()返回最終結果之前,此功能可以修改的結果文檔或替換的結果文檔作為一個整體。 |
二.Mongo VUE操作Group By 1.MonogoDB資料庫中添加了訂單的資料
[html] view plain copy
- /* 0 */
- {
- "_id" : ObjectId("552a330e05c27486b9b9b650"),
- "_class" : "com.mongo.model.Orders",
- "onumber" : "002",
- "date" : ISODate("2014-01-03T16:03:00Z"),
- "cname" : "zcy",
- "item" : {
- "quantity" : 1,
- "price" : 4.0,
- "pnumber" : "p002"
- }
- }
-
- /* 1 */
- {
- "_id" : ObjectId("552a331d05c275d8590a550d"),
- "_class" : "com.mongo.model.Orders",
- "onumber" : "003",
- "date" : ISODate("2014-01-04T16:03:00Z"),
- "cname" : "zcy",
- "item" : {
- "quantity" : 10,
- "price" : 2.0,
- "pnumber" : "p001"
- }
- }
-
- /* 2 */
- {
- "_id" : ObjectId("552a333105c2f28194045a72"),
- "_class" : "com.mongo.model.Orders",
- "onumber" : "003",
- "date" : ISODate("2014-01-04T16:03:00Z"),
- "cname" : "zcy",
- "item" : {
- "quantity" : 30,
- "price" : 4.0,
- "pnumber" : "p002"
- }
- }
-
- /* 3 */
- {
- "_id" : ObjectId("552a333f05c2b62c01cff50e"),
- "_class" : "com.mongo.model.Orders",
- "onumber" : "004",
- "date" : ISODate("2014-01-05T16:03:00Z"),
- "cname" : "zcy",
- "item" : {
- "quantity" : 5,
- "price" : 4.0,
- "pnumber" : "p002"
- }
- }
2.MongoDB實現分組並統計
1)我們要對日期和產品編碼進行分組,並計算相同的產品的數量
Sql語句:Select date, pnumber,sum(quantity) as total from orders,items group by date, pnumber(少了兩張表的關聯的條件)
MongoDB:
db.orders.group({
key: { date:1,‘item.pnumber‘:1},
initial : {"total":0},
reduce : function Reduce(doc, out) {
out.total+=doc.item.quantity
} });
結果:
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
returnout;
}
});
結果:
3)keyf的使用
keyf 對日期進行處理並以作為key來進來分組
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
returnout;
}
});
結果:
三.Java MongoDB 實現 Spring Data MongoDB 提供了Group有幾個介面 GroupCommand groupCommand=new GroupCommand(inputCollection, keys, condition, initial, reduce, finalize);
1)我們要對日期和產品編碼進行分組,並計算相同的產品的數量
[java] view plain copy
- <strong> </strong> @Override
- public void getGroupCount(String collectionName) {
-
- BasicDBObject key = new BasicDBObject();
- key.put("date", 1);
- key.put("item.pnumber", 1);
- //條件
- BasicDBObject cond = new BasicDBObject();
- //初始化
- BasicDBObject initial = new BasicDBObject();
- initial.append("total", 0);
- //reduce
- String reduce = "function Reduce(doc, out) { " +
- " out.total+=doc.item.quantity;" +
- "}";
-
- SimpleDateFormat format=new SimpleDateFormat("yyyy-mm-dd");
- BasicDBList groupList=(BasicDBList) mongoTemplate.getCollection(collectionName).group(key, cond, initial, reduce);
- if(groupList!=null&&groupList.size()>0){
- System.out.println("date item.pnumber total");
- for(int i=0;i<groupList.size();i++){
- BasicDBObject obj=(BasicDBObject) groupList.get(i);
- System.out.println(format.format(obj.getDate("date"))+" "+obj.getString("item.pnumber")+" "+obj.getInt("total"));
- }
- }
- }
結果:
2)實現一天賣出了多少個產品,金額是多少,平均價格是多少
[java] view plain copy
- @Override
- public void getGroupAvg(String collectionName) {
-
- BasicDBObject key = new BasicDBObject();
- key.put("date", 1);
-
- //條件
- BasicDBObject cond = new BasicDBObject();
- //初始化
- BasicDBObject initial = new BasicDBObject();
- initial.append("total", 0);
- initial.append("money", 0.0);
-
- //reduce
- String reduce = "function Reduce(doc, out) { " +
- " out.total+=doc.item.quantity;" +
- " out.money+=doc.item.quantity*doc.item.price;" +
- "}";
-
- String finalize="function Finalize (out) { " +
- " out.avg=out.money/out.total;" +
- " return out;" +
- "}";
- SimpleDateFormat format=new SimpleDateFormat("yyyy-mm-dd");
- BasicDBList groupList=(BasicDBList) mongoTemplate.getCollection(collectionName).group(key, cond, initial, reduce, finalize);
- if(groupList!=null&&groupList.size()>0){
- System.out.println("date total money avg");
- for(int i=0;i<groupList.size();i++){
- BasicDBObject obj=(BasicDBObject) groupList.get(i);
- System.out.println(format.format(obj.getDate("date"))+" "+obj.getInt("total")+" "+obj.getInt("money")+" "+obj.getDouble("avg"));
- }
- }
-
- }
結果:
MongoDB 彙總Group(一)