首页 > 解决方案 > mongodb存储桶边界大于查询

问题描述

我有 mongodb 存储桶查询来获取下限和上限计数。现在我想要大于查询来显示 > 10%、> 20%、>50%、>60% 等的折扣。

我不知道如何包含大于边界。或使用任何其他方式来计算报价?

db.getCollection('product').aggregate([{$facet: {
"offers":[{$unwind:"$variants"},
{$match:{"variants.prices.discount_percent": { $exists: 1 }, 
"variants.is_published":{"$ne":false}}},
{
      $bucket: {
        groupBy: "$variants.prices.discount_percent",
        boundaries: [  0, 20, 30, 40, 100,Infinity ],

        output: {
          "count": { $sum: 1 }
        }
      }      }
  ]}}

  ])

在这种情况下,我想要的是折扣总数。所以 20% 的折扣包括 30% 以上的折扣,30% 包括 40% 以上的折扣等等。

输出应该是:

20% - (10), 30% - (5), 40% - (2) 等等。

这里 20% 的计数是 10,其中也包括 5 和 2。因为它 >= 20。30% 包括 40% 的计数 2,因为它 >= 30。

标签: mongodbmongodb-queryaggregation-framework

解决方案


推荐阅读