在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)庫/ MongoDB聚合
MongoDB教程
MongoDB覆蓋查詢
MongoDB數(shù)據(jù)建模
MongoDB聚合
MongoDB更改用戶密碼和自定義數(shù)據(jù)
MongoDB用戶
MongoDB分片
MongoDB創(chuàng)建集合
MongoDB文本搜索
MongoDB數(shù)據(jù)類型
MongoDB GridFS
MongoDB索引限制
MongoDB插入文檔
MongoDB刪除集合
MongoDB數(shù)據(jù)庫引用
MongoDB復(fù)制
MongoDB Map Reduce
Python連接MongoDB操作
MongoDB原子操作
MongoDB特點
MongoDB安全檢查表
MongoDB排序記錄
MongoDB自動遞增序列
MongoDB安裝配置(Windows)
MongoDB備份與恢復(fù)
MongoDB安裝配置(Ubuntu)
Ruby連接MongoDB操作
MongoDB部署
MongoDB索引
MongoDB分析查詢
MongoDB投影(選擇字段)
MongoDB刪除數(shù)據(jù)庫
MongoDB認(rèn)證
MongoDB限制記錄數(shù)
MongoDB添加用戶
MongoDB固定循環(huán)集合
MongoDB高級索引
MongoDB數(shù)據(jù)庫的優(yōu)點
MongoDB快速入門
MongoDB創(chuàng)建數(shù)據(jù)庫
MongoDB啟用身份驗證
MongoDB歷史
MongoDB管理用戶和角色
MongoDB安裝配置(RedHat/CentOS)
MongoDB刪除文檔
Java連接MongoDB操作
MongoDB正則表達(dá)式
MongoDB查詢文檔
MongoDB關(guān)聯(lián)關(guān)系
PHP連接MongoDB操作
MongoDB更新文檔
MongoDB ObjectId

MongoDB聚合

聚合操作處理數(shù)據(jù)記錄并返回計算結(jié)果。 聚合操作將多個文檔中的值組合在一起,并可對分組數(shù)據(jù)執(zhí)行各種操作,以返回單個結(jié)果。 在SQL中的 count(*)group by組合相當(dāng)于mongodb 中的聚合功能。

aggregate()方法

對于MongoDB中的聚合,應(yīng)該使用aggregate()方法。

語法

aggregate()方法的基本語法如下 -

>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

示例

假設(shè)在集合中,有以下數(shù)據(jù) -

db.article.insert([
{
   _id: 100,
   title: 'MongoDB Overview',
   description: 'MongoDB is no sql database',
   by_user: 'Maxsu',
   url: 'http://www.yiibai.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
},
{
   _id: 101,
   title: 'NoSQL Overview', 
   description: 'No sql database is very fast',
   by_user: 'Maxsu',
   url: 'http://www.yiibai.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 10
},
{
   _id: 102,
   title: 'Neo4j Overview', 
   description: 'Neo4j is no sql database',
   by_user: 'Kuber',
   url: 'http://www.neo4j.com',
   tags: ['neo4j', 'database', 'NoSQL'],
   likes: 750
},
{
   _id: 103,
   title: 'MySQL Overview', 
   description: 'MySQL is sql database',
   by_user: 'Curry',
   url: 'http://www.yiibai.com/mysql/',
   tags: ['MySQL', 'database', 'SQL'],
   likes: 350
}])

現(xiàn)在從上面的集合中,如果要顯示一個列表,說明每個用戶寫入了多少個教程,那么可使用以下aggregate()方法 -

> db.article.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{ "_id" : "Curry", "num_tutorial" : 1 }
{ "_id" : "Kuber", "num_tutorial" : 1 }
{ "_id" : "Maxsu", "num_tutorial" : 2 }
>

對于上述用例的Sql等效查詢是:

select by_user, count(*) as num_tutorial from `article` group by by_user;

在上面的例子中,我們按字段by_user分組了文檔,并且每次發(fā)生的by_user的前一個值的值都被遞增。以下是可用聚合表達(dá)式的列表。

表達(dá)式 描述 示例
$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 從集合中的所有文檔獲取相應(yīng)值的最小值。 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
$max 從集合中的所有文檔獲取相應(yīng)值的最大值。 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
$push 將值插入到生成的文檔中的數(shù)組中。 db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])
$addToSet 將值插入生成的文檔中的數(shù)組,但不會創(chuàng)建重復(fù)項。 db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
$first 根據(jù)分組從源文檔獲取第一個文檔。 通常情況下,這只適用于以前應(yīng)用的“$sort”階段。 db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])
$last 根據(jù)分組從源文檔獲取最后一個文檔。通常情況下,這只適用于以前應(yīng)用的“$sort”階段。 db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])

管道概念

在UNIX命令中,shell管道可以對某些輸入執(zhí)行操作,并將輸出用作下一個命令的輸入。 MongoDB也在聚合框架中支持類似的概念。每一組輸出可作為另一組文檔的輸入,并生成一組生成的文檔(或最終生成的JSON文檔在管道的末尾)。這樣就可以再次用于下一階段等等。

以下是在聚合框架可能的階段 -

  • $project - 用于從集合中選擇一些特定字段。
  • $match - 這是一個過濾操作,因此可以減少作為下一階段輸入的文檔數(shù)量。
  • $group - 這是上面討論的實際聚合。
  • $sort - 排序文檔。
  • $skip - 通過這種方式,可以在給定數(shù)量的文檔的文檔列表中向前跳過。
  • $limit - 限制從當(dāng)前位置開始的給定數(shù)量的文檔數(shù)量。
  • $unwind - 用于展開正在使用數(shù)組的文檔。使用數(shù)組時,數(shù)據(jù)是預(yù)先加入的,此操作將被撤銷,以便再次單獨使用文檔。 因此,在這個階段,將增加下一階段的文件數(shù)量。