1
這裏是我的MongoDB集合模式:MongoDB在不同文檔中的數組中的項目聚合計數?
company: String
model: String
tags: [String]
我需要聚合,所以我得到以下輸出:
[{
"_id": {
"company": "Lenovo",
"model": "T400"
},
"tags": {
tag: "SomeTag"
count: 124 // number of times, this tag was found in `Lenovo T400`
}
}...]
我試着做到以下幾點:
var aggParams = {};
aggParams.push({ $unwind: '$tags' });
aggParams.push({ $group: {
_id: { company: '$company', model: '$model' },
tags: { $push: { tag: '$tags', count: { $sum: 1 } } },
}});
但我得到以下錯誤:
invalid operator '$sum'
與aggregation
這樣做的正確方法是什麼?
您的數據與您的模式相矛盾。什麼是實際數據中的「標籤」?一個字符串?還是數組? –
'[String]' - 表示** **字符串數組** –