2

這裏是我的MongoDB集合模式:MongoDB不同文檔中兩個數組中項目的聚合計數?

company: String 
model: String 
cons: [String] // array of tags that were marked as "cons" 
pros: [String] // array of tags that were marked as "pros" 

我需要聚合,所以我得到以下輸出:

[{ 
    "_id": { 
    "company": "Lenovo", 
    "model": "T400" 
    }, 
    "tags": { 
    tag: "SomeTag" 
    pros: 124 // number of times, "SomeTag" tag was found in "pros" array in `Lenovo T400` 
    cons: 345 // number of times, "SomeTag" tag was found in "cons" array in `Lenovo T400` 
    } 
}...] 

我試着做到以下幾點:

var aggParams = {}; 
aggParams.push({ $unwind: '$cons' }); 
aggParams.push({ $unwind: '$pros' }); 
aggParams.push({$group: { 
    _id: { 
    company: '$company', 
    model: '$model', 
    consTag: '$cons' 
    }, 
    consTagCount: { $sum: 1 } 
}}); 
aggParams.push({$group: { 
    _id: { 
    company: '$_id.company', 
    model: '$_id.model', 
    prosTag: '$pros' 
    }, 
    prosTagCount: { $sum: 1 } 
}}); 
aggParams.push({$group: { 
    _id: { 
    company:'$_id.company', 
    model: '$_id.model' 
    }, 
    tags: { $push: { tag: { $or: ['$_id.consTag', '$_id.prosTag'] }, cons: '$consTagCount', pros: '$prosTagCount'} } 
}}); 

但我得到了以下結果:

{ 
    "_id": { 
    "company": "Lenovo", 
    "model": "T400" 
    }, 
    "tags": [ 
    { 
     "tag": false, 
     "pros": 7 
    } 
    ] 
} 

aggregation做什麼正確的方法是什麼?

+0

我們可以在這裏假設,「優點」或「缺點」中的「標記」在兩個陣列中都是唯一的,或者在兩者中都是唯一的?當然每個文件。 –

+0

正確,每個文檔的標籤都是唯一的(兩個數組中的「唯一」) –

+0

所以要完全清楚。 「SomeTag」只會出現在每個文檔只有一次「贊成」而不是「缺點」一次? –

回答

2

是的,考慮到存在多個數組,並且如果同時嘗試兩種方法,最終得到一個「笛卡爾條件」,其中一個arrray乘以另一個的內容。

Model.aggregate(
    [ 
     { "$project": { 
      "company": 1, 
      "model": 1, 
      "data": { 
       "$setUnion": [ 
        { "$map": { 
         "input": "$pros", 
         "as": "pro", 
         "in": { 
          "type": { "$literal": "pro" }, 
          "value": "$$pro" 
         } 
        }}, 
        { "$map": { 
         "input": "$cons", 
         "as": "con", 
         "in": { 
          "type": { "$literal": "con" }, 
          "value": "$$con" 
         } 
        }} 
       ] 
      } 
     }}, 
     { "$unwind": "$data" } 
     { "$group": { 
      "_id": { 
       "company": "$company", 
       "model": "$model", 
       "tag": "$data.value" 
      }, 
      "pros": { 
       "$sum": { 
        "$cond": [ 
         { "$eq": [ "$data.type", "pro" ] }, 
         1, 
         0 
        ] 
       } 
      }, 
      "cons": { 
       "$sum": { 
        "$cond": [ 
         { "$eq": [ "$data.type", "con" ] }, 
         1, 
         0 
        ] 
       } 
      } 
     } 
    ], 
    function(err,result) { 

    } 
) 

所以通過第一$project階段$map運營商都加入了「:

因此,剛一開始,這可能表明,你應該如何存儲數據在第一時間結合陣列內容爲每個數組的每個項目鍵入「值」。並不是說這裏真的很重要,因爲無論如何所有的項目都應該處理「獨特的」,運算符將每個數組「排列」成單數組。

如前所述,您可能應該首先以這種方式進行存儲。

則過程接着$group,其中每個「贊成」和「缺點」,然後通過$cond評估,以用於它的匹配「類型」,要麼返回10其中匹配是分別true/false$sum聚合累加器$unwind

這爲您提供了一個「邏輯匹配」,根據指定的分組鍵,對聚合操作中的各個「類型」進行計數。

+0

這裏是輸出: '{ 「_id」:{ 「公司」: 「聯想」, 「模式」: 「T400」, 「標籤」: 「質量」 }, 「優點」:132 , 「cons」:324 }' –

+2

@MikeVayvala那麼?有什麼意義?這應該是你想要的,儘管你的「需求輸出」不再是真正的理想。按照分組鍵計算每個「pro」和「con」。這基本上就是你要求的。 –

相關問題