首页 > 解决方案 > MongoDB ODM 聚合生成器(最小值,最大值)- 跳过 0

问题描述

我有一个订单表:

{ "_id" : 1, "customer" : "1", price: 0 }
{ "_id" : 2, "customer" : "1", price: 100 }
{ "_id" : 3, "customer" : "1", price: 120 }
{ "_id" : 4, "customer" : "2", price: 150 }

我想获得每个客户的最低订单价值。

$builder
    ->facet()
        ->field('customerOrders')
        ->pipeline(
           $dm->createAggregationBuilder('Document\Order')->group()
               ->field('id')
               ->expression('$customer')
               ->field('lowestValue')
               ->min('$price')
               ->field('highestValue')
               ->max('$price')
);

上面的代码有效。

{ "_id" : "1", "lowestValue" : 0,   "highestValue" : 120 }
{ "_id" : "2", "lowestValue" : 150, "highestValue" : 150 }

我想忽略价格为 0 或 null 的订单。

期望的结果:

{ "_id" : "1", "lowestValue" : 100, "highestValue" : 120 }
{ "_id" : "2", "lowestValue" : 150, "highestValue" : 150 }

这可能吗?
我可以使用 $cond (aggregation) 吗?

{ $cond: [ <boolean-expression>, <true-case>, <false-case> ] }

https://docs.mongodb.com/manual/reference/operator/aggregation/cond/

MongoDB 4.2
MongoDB ODM 2.0.3

标签: mongodbodm

解决方案


只需以 $gt : 0.... 开始您的管道,它以有效文档开始数据集...


推荐阅读