2016-11-03 47 views
1

我想計算特定項目中的所有電子郵件(ID:7),但忽略一個廣告系列中的重複行。忽略重複文檔的計數

這裏是我的收藏結構的例子:

{ 
    "_id" : ObjectId("581a9054c274f7b512e8ed94"), 
    "email" : "[email protected]", 
    "IDproject" : 7, 
    "IDcampaign" : 10 
} 

{ 
    "_id" : ObjectId("581a9064c274f7b512e8ed95"), 
    "email" : "[email protected]", 
    "IDproject" : 7, 
    "IDcampaign" : 10 
} 

{ 
    "_id" : ObjectId("581a9068c274f7b512e8ed96"), 
    "email" : "[email protected]", 
    "IDproject" : 7, 
    "IDcampaign" : 10 
} 

{ 
    "_id" : ObjectId("581a906cc274f7b512e8ed97"), 
    "email" : "[email protected]", 
    "IDproject" : 7, 
    "IDcampaign" : 11 
} 

{ 
    "_id" : ObjectId("581a9072c274f7b512e8ed98"), 
    "email" : "[email protected]", 
    "IDproject" : 7, 
    "IDcampaign" : 11 
} 

{ 
    "_id" : ObjectId("581a9079c274f7b512e8ed99"), 
    "email" : "[email protected]", 
    "IDproject" : 7, 
    "IDcampaign" : 12 
} 

這是結果應該是什麼:

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

Total: 5(6)

。請注意, [email protected]被提及兩次。這是因爲 [email protected]的廣告活動10,10和11我們忽略了一個10

這是我已經試過:

db.mycollection.aggregate([ 
    {$match : {IDproject : 7}}, 
    {$group : {_id : "$email", total : {$sum : 1}}} 
]) 

但只返回獨特的電子郵件忽略IDcampaign。另外,我可以收到電子郵件的唯一的號碼用下面的查詢:

​​

但同樣,它只顯示獨特的電子郵件忽略IDcampaign

有人可以給我一個提示如何計算電子郵件,包括IDcampaign

謝謝。

p.s.我使用MongoDB和PHP,我可以用PHP計算來解決這個問題,但這不是解決方案。

回答

3

把它作爲你組鍵的一部分,如下面的例子:

db.mycollection.aggregate([ 
    { "$match": { "IDproject": 7 } }, 
    { 
     "$group": { 
      "_id": { 
       "email" : "$email",     
       "IDcampaign" : "$IDcampaign" 
      }, 
      "count": { "$sum": 1 } 
     } 
    } 
]) 

樣本輸出

/* 1 */ 
{ 
    "_id" : { 
     "email" : "[email protected]", 
     "IDcampaign" : 10 
    }, 
    "count" : 1 
} 

/* 2 */ 
{ 
    "_id" : { 
     "email" : "[email protected]", 
     "IDcampaign" : 12 
    }, 
    "count" : 1 
} 

/* 3 */ 
{ 
    "_id" : { 
     "email" : "[email protected]", 
     "IDcampaign" : 11 
    }, 
    "count" : 1 
} 

/* 4 */ 
{ 
    "_id" : { 
     "email" : "[email protected]", 
     "IDcampaign" : 10 
    }, 
    "count" : 2 
} 

/* 5 */ 
{ 
    "_id" : { 
     "email" : "[email protected]", 
     "IDcampaign" : 11 
    }, 
    "count" : 1 
} 

要回答你的後續問題上獲得只計算因爲您不需要電子郵件列表,您可以運行以下管道

db.mycollection.aggregate([ 
    { "$match": { "IDproject": 7 } }, 
    { 
     "$group": { 
      "_id": null, 
      "count": { "$sum": 1 }, 
      "emails": { 
       "$addToSet": { 
        "email" : "$email",     
        "IDcampaign" : "$IDcampaign" 
       } 
      } 
     } 
    }, 
    { 
     "$project": { 
      "_id": 0, 
      "count": 1, 
      "total": { "$size": "$emails" } 
     } 
    } 
]) 

它給你的結果

{   
    "total" : 5, 
    "count" : 6 
} 

,你可以解釋爲Total 5 (of 6)

+2

是的,然後你可以使用$項目階段來重塑你的輸出。 例如:{$ project:{email:「$ _id.email」,IDcampaign:「$ _id.IDcampaign」,總數:「$ total」,_id:0}} – dyouberg

+2

@dyouberg不添加'$ project'只是爲了重塑這裏的文件。這是沒有必要的,並會導致性能下降。 – styvane

+0

@chridam我太親近了!這很棒。謝謝。 – Nedim