Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB ODM聚合生成器(最小值、最大值)-跳过0_Mongodb_Odm - Fatal编程技术网

MongoDB ODM聚合生成器(最小值、最大值)-跳过0

MongoDB ODM聚合生成器(最小值、最大值)-跳过0,mongodb,odm,Mongodb,Odm,我有一张订单表: { "_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('cust

我有一张订单表:

{ "_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或空的订单

预期结果:

{ "_id" : "1", "lowestValue" : 100, "highestValue" : 120 }
{ "_id" : "2", "lowestValue" : 150, "highestValue" : 150 }
这可能吗?
我可以使用$cond(聚合)吗

{$cond:[,]}

MongoDB 4.2

MongoDB ODM 2.0.3

只需以$gt:0开始您的销售渠道…即以有效文档启动数据集…

正如Cahaba data建议的那样,在分组前过滤出价格为0的订单

db.orders.aggregate([
{
$match:{price:{$gt:0}
},
{
$group:{
_id:“$customer”,
最低值:{$min:$price},
最高值:{$max:$price}
}
}
])

感谢您的回复。我找到了另一个解决办法

db.orders.aggregate([
  {
    $group: {
      _id: "$customer",
      lowestValue: {
        $min: {
          $cond: [{
              $gt: ["$price", 0]
            }, "$price", null]
        }
      }
    }
  }
]);
条令聚合生成器

db.orders.aggregate([
  {
    $group: {
      _id: "$customer",
      lowestValue: {
        $min: {
          $cond: [{
              $gt: ["$price", 0]
            }, "$price", null]
        }
      }
    }
  }
]);
->field('lowestValue')
->min($builder->expr()->cond($builder->expr()->gt('$price', 0), '$price', null))