如何为聚合获取前n个桶,将所有其他桶合并到“其他”桶中?

时间:2016-01-11 05:45:28

标签: mongodb mongodb-query pymongo aggregation-framework

假设一个集合,其架构如下所示:

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

现在,我希望获得前5名客户(按购买排名),第6名桶是“其他”,它结合了其他客户的所有购买数量。

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

{ "_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 个答案:

答案 0 :(得分:1)

您想要的是weighting。为此,您需要通过$project并使用$cond运算符为文档添加权重,然后按&#34; weight&#34;对它们进行排序。在提升他人和购买数量&#34;按降序排列。

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 }