聚合组结果总体最小值和最大值,我的对象存储到我的收藏品,看起来像这样:查找单查询
[{
"_id" : ObjectId("56bcbe570793be2e5025567e"),
"CarPlate" : "AB123CD",
"Brand" : "BMW",
"Version" : "316d",
"Description" : "BMW 316d",
"Prices" : [
{
"PriceType" : NumberInt(0),
"VatType" : NumberInt(0),
"Currency" : "EUR",
"Value" : 20100.0
},
{
"PriceType" : NumberInt(3),
"VatType" : NumberInt(0),
"Currency" : "EUR",
"Value" : 23900.0
},
{
"PriceType" : NumberInt(4),
"VatType" : null,
"Currency" : "EUR",
"Value" : 30950.0
}
]
}, {
"_id" : ObjectId("56bcbe570793be2e5025567f”),
"CarPlate" : "AB123CE”,
"Brand" : "BMW",
"Version" : "318d",
"Description" : "BMW 318d",
"Prices" : [
{
"PriceType" : NumberInt(0),
"VatType" : NumberInt(0),
"Currency" : "EUR",
"Value" : 23900.0
},
{
"PriceType" : NumberInt(3),
"VatType" : NumberInt(0),
"Currency" : "EUR",
"Value" : 23900.0
},
{
"PriceType" : NumberInt(4),
"VatType" : null,
"Currency" : "EUR",
"Value" : 40250.0
}
]
}]
我想检索最小和最大的价格值[对于整个集合],其中PriceType等于4使用单个查询。
我找到了一种方法来做到这一点,但运行两个不同的查询(基本上是相同的查询,但使用不同的排序)
db.Cars.aggregate(
[
{
$unwind: '$Prices'
},
{
$match: {
'Prices.PriceType': 4
}
},
{
$group: {
_id: '$CarPlate',
valueMin: { $min: '$Prices.Value' },
valueMax: { $max: '$Prices.Value' }
}
},
{
$sort: {
valueMin: 1
}
},
{
$limit: 1
}
]
);
任何提示?
澄清你想找到的最小值和最大值为每车/整个集合中的文档/对象?或整个集合中的一分钟和一个最大值 – ibininja
到整个集合 – imperugo
因此,根据您的示例数据,您希望得到类似这样的结果? '{min:20100.0,max:40250.0}' –