MongoDB 聚合
MongoDB 聚合
MongoDB 中聚合(aggregate)主要用於處理資料(諸如統計平均值,求和等),並返回計算後的資料結果。
有點類似 SQL 語句中的 count(*)。
aggregate() 方法
MongoDB中聚合的方法使用aggregate()。
語法
aggregate() 方法的基本語法格式如下所示:
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
例項
集合中的資料如下:
{ _id: ObjectId(7df78ad8902c) title: 'MongoDB Overview', description: 'MongoDB is no sql database', by_user: 'itread01.com', url: 'http://www.itread01.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }, { _id: ObjectId(7df78ad8902d) title: 'NoSQL Overview', description: 'No sql database is very fast', by_user: 'itread01.com', url: 'http://www.itread01.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 10 }, { _id: ObjectId(7df78ad8902e) title: 'Neo4j Overview', description: 'Neo4j is no sql database', by_user: 'Neo4j', url: 'http://www.neo4j.com', tags: ['neo4j', 'database', 'NoSQL'], likes: 750 },
現在我們通過以上集合計算每個作者所寫的文章數,使用aggregate()計算結果如下:
> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}]) { "result" : [ { "_id" : "itread01.com", "num_tutorial" : 2 }, { "_id" : "Neo4j", "num_tutorial" : 1 } ], "ok" : 1 } >
以上例項類似sql語句:
select by_user, count(*) from mycol group by by_user
在上面的例子中,我們通過欄位 by_user 欄位對資料進行分組,並計算 by_user 欄位相同值的總和。
下表展示了一些聚合的表示式:
表示式 | 描述 | 例項 |
---|---|---|
$sum | 計算總和。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}]) |
$avg | 計算平均值 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}]) |
$min | 獲取集合中所有文件對應值得最小值。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}]) |
$max | 獲取集合中所有文件對應值得最大值。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}]) |
$push | 在結果文件中插入值到一個數組中。 | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}]) |
$addToSet | 在結果文件中插入值到一個數組中,但不建立副本。 | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}]) |
$first | 根據資源文件的排序獲取第一個文件資料。 | db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}]) |
$last | 根據資源文件的排序獲取最後一個文件資料 | db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}]) |
管道的概念
管道在Unix和Linux中一般用於將當前命令的輸出結果作為下一個命令的引數。
MongoDB的聚合管道將MongoDB文件在一個管道處理完畢後將結果傳遞給下一個管道處理。管道操作是可以重複的。
表示式:處理輸入文件並輸出。表示式是無狀態的,只能用於計算當前聚合管道的文件,不能處理其它的文件。
這裡我們介紹一下聚合框架中常用的幾個操作:
- $project:修改輸入文件的結構。可以用來重新命名、增加或刪除域,也可以用於建立計算結果以及巢狀文件。
- $match:用於過濾資料,只輸出符合條件的文件。$match使用MongoDB的標準查詢操作。
- $limit:用來限制MongoDB聚合管道返回的文件數。
- $skip:在聚合管道中跳過指定數量的文件,並返回餘下的文件。
- $unwind:將文件中的某一個數組型別欄位拆分成多條,每條包含陣列中的一個值。
- $group:將集合中的文件分組,可用於統計結果。
- $sort:將輸入文件排序後輸出。
- $geoNear:輸出接近某一地理位置的有序文件。
管道操作符例項
1、$project例項
db.article.aggregate( { $project : { title : 1 , author : 1 , }} );
這樣的話結果中就只還有_id,tilte和author三個欄位了,預設情況下_id欄位是被包含的,如果要想不包含_id話可以這樣:
db.article.aggregate( { $project : { _id : 0 , title : 1 , author : 1 }});
2.$match例項
db.articles.aggregate( [ { $match : { score : { $gt : 70, $lte : 90 } } }, { $group: { _id: null, count: { $sum: 1 } } } ] );
$match用於獲取分數大於70小於或等於90記錄,然後將符合條件的記錄送到下一階段$group管道操作符進行處理。
3.$skip例項
db.article.aggregate( { $skip : 5 });
經過$skip管道操作符處理後,前五個文件被"過濾"掉。