本文介绍了我如何才能获得前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个存储桶进行汇总,以及如何将所有其他存储桶合并成一个“其他"存储桶?桶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 04:21