How can I get top n buckets for an aggregation and all other buckets combined into an “other” bucket?

自闭症网瘾萝莉.ら 提交于 2020-01-11 05:16:50

问题


Assume a collection with schema like as shown below:

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

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}

回答1:


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 } } 
])

Which returns:

{ "_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 }


来源:https://stackoverflow.com/questions/34714910/how-can-i-get-top-n-buckets-for-an-aggregation-and-all-other-buckets-combined-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!