[教程]MongoDB 從入門到進階 (aggregation數據庫狀態)


    下載地址: http://www.wojilu.com/Forum1/Topic/4601

     GitHub:  https://github.com/magicdict/MagicMongoDBTool

     在開始介紹這節之前,我來說說我開發這個工具的意義。

     我不敢保證以后MongoDB可以大紅大紫,也無法保證MongoDB以后會分享很多很多市場份額。

     現在開發這個東西,一年后可能Mongo倒閉了,這個工具就是一個垃圾。一年后,Mongo修成正果,這個工具就是香餑餑。

     等所有人都覺得這個東西會火,你才去關注,開發工具,就晚了。花點業余時間,開發Mongo工具的同時,最主要去學習一下大數據的一些思想,例如副本,分片概念。

     還有就是階層數據庫的一些實現的方法。當然,由於3年前才開始從VB.NET轉C#的,也希望通過多些編碼將C#用身體記住。

[aggregation Framework]

例子1

http://docs.mongodb.org/manual/reference/aggregation/

aggregation Framework 是MongoDB的一個功能強大的統計框架,這里就演示一下這個框架的使用方法。

首先,我們新建一個aggregation數據集。然后再按照官方文檔的樣子,使用MongoCola插入一個文檔。(將下面這段JSON直接粘貼到新建文檔的窗體里面就可以了)

{
  title : "this is my title" ,
  author : "bob" ,
  posted : new Date() ,
  pageViews : 5 ,
  tags : [ "fun" , "good" , "fun" ] ,
  comments : [
      { author :"joe" , text : "this is cool" } ,
      { author :"sam" , text : "this is bad" }
  ],
  other : { foo : 5 }
}

 

1.$project

這個操作符號,可以用來做Select 的操作。用來指定顯示的列。

 { $project : {
        title : 1 ,
        author : 1 
    }}

這里表示我們只是需要title和author列的信息。

這里的參數1:表示 0:非表示。 _id是一個默認的表示項目,我們可以通過將_id設置為0,告訴系統不表示這個項目。

 { $project : {
        _id : 0 ,
        title : 1 ,
        author : 1
    }}

當然,你也可以新建一個字段,例如一個doctoredPageViews字段,這個字段是在原有字段的基礎上加工而成的值。

就如 SQL中的,Select (pageViews + 10 As doctoredPageViews) from table

{ $project : {
        title : 1,
        doctoredPageViews : { $add:["$pageViews", 10] }
    }}

 當然你也可以重新命名列名

 { $project : {
        title : 1 ,
        page_views : "$pageViews" ,
        bar : "$other.foo"
    }}

最后你也可以通過Project操作符,來構建你想要的文檔結果。下面這個例子里面的Status就是一個新建的子文檔

{ $project : {
        title : 1 ,
        stats : {
            pv : "$pageViews",
            foo : "$other.foo",
            dpv : { $add:["$pageViews", 10] }
        }
    }}

2$match
這個操作符號,有點類似於Select文的Where條件。
例如我們想尋找author是bob的記錄:
{ $match : { author : "bob" } }

這個Aggregate框架里面的各種操作符號是可以任意組合的。我們可以既有match操作,又有project操作。

這里我們用工具將兩種操作的條件分別加上。

注意:這里我們已經添加了2個條件了。第一個是match,第二個是project。

 

例子2

http://dotnet.dzone.com/articles/mongodb-aggregation-framework

下面是例子的測試數據

{ "_id" : "1", "User" : "Tom", "Country" : "Finland", "Count" : 1 }
{ "_id" : "2", "User" : "Tom", "Country" : "Finland", "Count" : 3 }
{ "_id" : "3", "User" : "Tom", "Country" : "Finland", "Count" : 2 }
{ "_id" : "4", "User" : "Mary", "Country" : "Sweden", "Count" : 1 }
{ "_id" : "5", "User" : "Mary", "Country" : "Sweden", "Count" : 7 }

 

我們找一下是User是Tom,Count >= 2 的記錄

{ $match : { 
     User: "Tom",
     Count:{"$gte": 2} 
    }
 }

4

關於操作符號:  http://docs.mongodb.org/manual/reference/aggregation/#comparison-operators

3 $Group

我們對於User進行Group操作,並且對於Count進行合計

注意,這里必須要有一個_id字段來表示Group的條件

 { $group : {
        _id : { "MyUser": "$User" }
       TotalCount : { $sum : "$Count" }
       }
}

結果就是Mary的合計值是8,Tom則是6.

Select User As MyUser
SUM(Count) AS TotalCount
from table
group by User

 

我們再次添加一條記錄

{ "_id" : "1", "User" : "Tom", "Country" : "Finland", "Count" : 1 }
{ "_id" : "2", "User" : "Tom", "Country" : "Finland", "Count" : 3 }
{ "_id" : "3", "User" : "Tom", "Country" : "Finland", "Count" : 2 }
{ "_id" : "4", "User" : "Mary", "Country" : "Sweden", "Count" : 1 }
{ "_id" : "5", "User" : "Mary", "Country" : "Sweden", "Count" : 7 }
{ "_id" : "6", "User" : "Tom", "Country" : "England", "Count" : 3 }

我們按照User和Country來做GroupBY

 { $group : {
        _id : { "MyUser": "$User" ,
                  "Country":"$Country" 
                  },
       TotalCount : { $sum : "$Count" }
       }
}

 

Select User As MyUser
SUM(Count) AS TotalCount
from table
group by User,Country

 

最后,可以將match,group,project一起使用

Select User As MyUser
SUM(Count) AS TotalCount
from table
group by User,Country
Where User = ‘Tom’ And Count >= 2

注意:這里有一個管道的概念,前一個操作的結果,作為后一個操作的源頭。例如這里Match結果,就是下一個group的操作源頭。

4$Sort

對於結果,我們還可以進行Sort操作。

 { $group : {
        _id : { "MyUser": "$User" ,
                  "Country":"$Country" 
                  },
       TotalCount : { $sum : "$Count" }
       }
}

上面這段是對於User和Country的分組Group操作。我們如果需要按照TotalCount排序的話,只需要在這個結果之后,增加Sort操作。

由於管道的緣故,Sort的操作源頭就是前面的Group結果

 { $sort : {
        TotalCount:1
       }
}

Select User As MyUser
SUM(Count) AS TotalCount
from table
group by User,Country
Order by TotalCount

 

 【Mongo的數據庫狀態】

     連接Connection狀態

   

     數據庫狀態

    

數據集狀態


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM