[英]MongoDB Aggregation: calculation for every unique/distinct value
我從集合中得到了這個數據集
{
item: 124001
price: 6
},
{
item: 124001
price: 6
},
{
item: 124121
price: 16
},
{
item: 124121
price: 13
},
{
item:n
price: x
}
從代碼:
let INDX = [xxx,xxx,xxx,xxx, ..n]
auctions.aggregate([
{
$match: { item: { $in: INDX }}
}
問題就在它之后,在 $group 階段。 例如,我想為每個獨特/獨特的項目收到 $min、$max 或 $avg 'price'。
當我嘗試使用時:
{
$group: {
min_1: { $min: "$price",}
}
}
我從所有數據中只收到 $min,
[ { _id: 0, min_1: 0 } ]
但我需要類似的東西:
{ _id: 124119, min_1: 66500 },
{ _id: 124437, min_1: 26398 }
好的,這是一個簡單的答案:
只是不要忘記 _id 字段並在 $group 階段使用它,就像:
{
$group: {
_id: "$item",
min_1: {
$min: '$price',
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.