如何获得聚合的前 n 个存储桶,以及将所有其他存储桶组合成“其他"存储桶?桶? [英] How can I get top n buckets for an aggregation and all other buckets combined into an "other" bucket?

查看:33
本文介绍了如何获得聚合的前 n 个存储桶,以及将所有其他存储桶组合成“其他"存储桶?桶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个集合的模式如下所示:

Assume a collection with schema like as shown below:

{
    "customer" : <unique-id-for-customer>,
    "purchase" : <number>,
}

现在,我想获得前 5 位客户(按购买数量),第 6 个桶是其他",它结合了其他客户的所有购买数量.

Now, I want to get the the top 5 customers (by purchase-queantity) and the 6th bucket is "others" which combines all the purchase quantity from other customers.

基本上,聚合的输出应该是这样的:

Basically, the output of aggregation should be like:

{ "_id" : "customer100", "purchasequantity" : 4000000 }
{ "_id" : "customer5", "purchasequantity" : 81800 }
{ "_id" : "customer4", "purchasequantity" : 40900 }
{ "_id" : "customer3", "purchasequantity" : 440 }
{ "_id" : "customer1", "purchasequantity" : 300 }
{"_id" : "others", "purchasequantity" : 29999}

推荐答案

你想要的叫做 权重.为此,您需要通过 $project 为您的文档添加权重 使用它们并使用 $cond 运算符,然后按权重"升序排序,购买数量"降序排序.

What you want is called weighting. To do that you need to add a weight to your documents by $projecting them and using the $cond operator, then sort them by "weight" in ascending other and by "purchasequantity" in descending order.

db.collection.aggregate([
    { "$project": { 
        "purchasequantity": 1, 
        "w": { 
            "$cond": [ { "$eq": [ "$_id", "others" ] }, 1, 0 ] 
        } 
    }}, 
    { "$sort": { "w": 1, "purchasequantity": -1 } } 
])

返回:

{ "_id" : "customer100", "purchasequantity" : 4000000, "w" : 0 }
{ "_id" : "customer5", "purchasequantity" : 81800, "w" : 0 }
{ "_id" : "customer4", "purchasequantity" : 40900, "w" : 0 }
{ "_id" : "customer3", "purchasequantity" : 440, "w" : 0 }
{ "_id" : "customer1", "purchasequantity" : 300, "w" : 0 }
{ "_id" : "others", "purchasequantity" : 29999, "w" : 1 }

这篇关于如何获得聚合的前 n 个存储桶,以及将所有其他存储桶组合成“其他"存储桶?桶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆