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.2MongoDB 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))