簡介
MongoDB 中聚合(aggregate)主要用於處理數據(諸如統計平均值,求和等),並返回計算后的數據結果。
db.collection.aggregate()方法是基於數據處理的聚合管道,每個文檔通過一個由多個階段(stage)組成的管道,可以對每個階段的管道進行分組、過濾等功能,然后經過一系列的處理,輸出相應的結果。
- 可以多個管道,能方便的進行數據的處理。
- 聚合效率非常高。
- 每個階段管道限制為100MB的內存。可以設置allowDiskUse為true來在聚合管道節點把數據寫入臨時文件
- 可以返回一個指針(cursor),數據放在內存中,直接操作
- 輸出的結果只能保存在一個文檔中,BSON Document大小限制為16M。可以通過返回指針解決
常用操作
- $project:修改輸入文檔的結構。可以用來重命名、增加或刪除域,也可以用於創建計算結果以及嵌套文檔。
- $match:用於過濾數據,只輸出符合條件的文檔。$match使用MongoDB的標准查詢操作。
- $limit:用來限制MongoDB聚合管道返回的文檔數。
- $skip:在聚合管道中跳過指定數量的文檔,並返回余下的文檔。
- $unwind:將文檔中的某一個數組類型字段拆分成多條,每條包含數組中的一個值。
- $group:將集合中的文檔分組,可用於統計結果。
- $sort:將輸入文檔排序后輸出。
- $geoNear:輸出接近某一地理位置的有序文檔。
常見操作舉例
訂單表
db. orders.insert([
{
"onumber" : "001",
"date" : "2015-07-02",
"cname" : "zcy1",
"items" :[ {
"ino" : "001",
"quantity" :2,
"price" : 4.0
},{
"ino" : "002",
"quantity" : 4,
"price" : 6.0
}
]
},{
"onumber" : "002",
"date" : "2015-07-02",
"cname" : "zcy2",
"items" :[ {
"ino" : "003",
"quantity" :1,
"price" : 4.0
},{
"ino" : "002",
"quantity" :6,
"price" : 6.0
}
]
},{
"onumber" : "003",
"date" : "2015-07-02",
"cname" : "zcy2",
"items" :[ {
"ino" : "004",
"quantity" :3,
"price" : 4.0
},{
"ino" : "005",
"quantity" :1,
"price" : 6.0
}
]
},{
"onumber" : "004",
"date" : "2015-07-02",
"cname" : "zcy2",
"items" :[ {
"ino" : "001",
"quantity" :3,
"price" : 4.0
},{
"ino" : "003",
"quantity" :1,
"price" : 6.0
}
]
}
])
- 查詢訂單號為001,002,003中的訂單詳情各個產品賣出多少個,並且過濾掉數量小於1的產品
查詢操作示例
@Override
public void getAggregation() {
Set<String> onumberSet=new HashSet<String>();
onumberSet.add("001");
onumberSet.add("002");
onumberSet.add("003");
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("onumber").in(onumberSet)),
Aggregation.unwind("items"),
Aggregation.group("items.ino").sum("items.quantity").as("total"),
Aggregation.match(Criteria.where("total").gt(1))
);
AggregationResults<BasicDBObject> outputType=mongoTemplate.aggregate(agg,"orders", BasicDBObject.class);
for (Iterator<BasicDBObject> iterator = outputType.iterator(); iterator.hasNext();) {
DBObject obj =iterator.next();
System.out.println(obj.toString());
}