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

Nip*_*dar 6 mongodb pymongo mongodb-query aggregation-framework

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

{
    "customer" : <unique-id-for-customer>,
    "purchase" : <number>,
}
Run Code Online (Sandbox Code Playgroud)

现在,我希望获得前五名客户(按购买排名),第六名是"其他",它结合了其他客户的所有购买数量.

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

{ "_id" : "customer100", "purchasequantity" : 4000000 }
{ "_id" : "customer5", "purchasequantity" : 81800 }
{ "_id" : "customer4", "purchasequantity" : 40900 }
{ "_id" : "customer3", "purchasequantity" : 440 }
{ "_id" : "customer1", "purchasequantity" : 300 }
{"_id" : "others", "purchasequantity" : 29999}
Run Code Online (Sandbox Code Playgroud)

sty*_*ane 1

你想要的叫做加权。为此,您需要通过$projecting 并使用$cond运算符为文档添加权重,然后按“重量”升序排列,并按“购买数量”降序排列。

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

返回:

{ "_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 }
Run Code Online (Sandbox Code Playgroud)