mongodb - 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
解决方案
只需以 $gt : 0.... 开始您的管道,它以有效文档开始数据集...
推荐阅读
- join - 雪花:加入具有有效日期和旧记录的表显示为 NULL
- r - 如何修复networkD3(sankeyNetwork)中节点的不正确颜色?
- kubernetes - 单节点Microk8s多主接口无法访问
- kivy - 调试后 SpeechRecognition 不起作用
- html - 为什么父级上的 CSS 溢出属性会影响 IMG 子级的大小?
- linux - ls \ 给了我一个新的提示,这是为了什么?
- r - 服务员包的加载栏在使用后不会消失 [SHINY]
- php - 将非常大的 Blob 从 MySQL 流式传输到 PHP 并创建一个文件
- gitlab - Gitlab webhook 有效负载 - 检测服务台
- elixir - 在一行中对多个主题使用 String.replace()