Mongodb 使用单个查询在聚合组结果中查找总体最小值和最大值
我的集合中存储了一个对象,如下所示:Mongodb 使用单个查询在聚合组结果中查找总体最小值和最大值,mongodb,mongodb-.net-driver,Mongodb,Mongodb .net Driver,我的集合中存储了一个对象,如下所示: [{ "_id" : ObjectId("56bcbe570793be2e5025567e"), "CarPlate" : "AB123CD", "Brand" : "BMW", "Version" : "316d", "Description" : "BMW 316d", "Prices" : [ { "PriceType" : NumberInt(0)
[{
"_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",
max_price:{$max:"$Prices.Value"},
min_price:{$min:"$Prices.Value"}
}
},
{
$group:
{_id:null,
max_value:{$max:"$max_price"},
min_value:{$min:"$min_price"}
}
}
])
这将在整个集合中以最小值和最大值输出单个值 对于所有知道如何将这样的查询转换为c的人(再次感谢@ibininja),这里是c代码:
this.carRepository
收集
.Aggregate()
.匹配(priceTypeFilder)
.退绕(i=>i.价格)
.Match(x=>x.Prices.PriceType==PriceTypeEnum.Catalog)
.Group(key=>key.CarPlate,
g=>新的
{
_id=g.键,
MinPrice=g.Min(o=>o.Prices.Value),
MaxPrice=g.Max(o=>o.Prices.Value)
})
.Group(key=>key.\u id,
g=>新的
{
_id=(字符串)null,
MinPrice=g.Min(o=>o.MinPrice),
MaxPrice=g.Max(o=>o.MaxPrice)
})
.SingleAsync();
为了澄清您想在整个系列中找到每辆车/文档/对象的最小值和最大值?或者从整个集合到整个集合的一个最小值和一个最大值基于您的示例数据,您想要这样的结果吗<代码>{min:20100.0,max:40250.0}是的,没错。在所有的收藏品中,有一张价格最高、价格最低的唱片看起来不错!谢谢,让我尝试将其转换为c#但是在shell上运行它很好,很高兴它能帮助您。您可以通过在Prices.PricesType上添加索引,并在$unwind之前添加另一个$match来优化此查询,以仅保留数组中至少有一个Prices与此PricesType匹配的文档。当然,在放松后仍然需要相同的$match,但这一个不会从索引中受益。这将使$unwind+$匹配更快,但这取决于您的数据集。如果100%的数据的PriceType=4,则没有帮助。不幸的是,我所有的数据的PriceType=4
> db.Cars.aggregate([
{
$unwind:"$Prices"
},
{
$match:{"Prices.PriceType":4}
},
{
$group:
{_id:"$CarPlate",
max_price:{$max:"$Prices.Value"},
min_price:{$min:"$Prices.Value"}
}
},
{
$group:
{_id:null,
max_value:{$max:"$max_price"},
min_value:{$min:"$min_price"}
}
}
])
this.carRepository
.Collection
.Aggregate()
.Match(priceTypeFilder)
.Unwind<Car, CarToPrice>(i => i.Prices)
.Match(x => x.Prices.PriceType == PriceTypeEnum.Catalog)
.Group(key => key.CarPlate ,
g => new
{
_id = g.Key,
MinPrice = g.Min(o => o.Prices.Value),
MaxPrice = g.Max(o => o.Prices.Value)
})
.Group(key => key._id,
g => new
{
_id = (string)null,
MinPrice = g.Min(o => o.MinPrice),
MaxPrice = g.Max(o => o.MaxPrice)
})
.SingleAsync();