2016-02-12 90 views
1

聚合组结果总体最小值和最大值,我的对象存储到我的收藏品,看起来像这样:查找单查询

[{ 
    "_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 
    } 
    ] 
); 

任何提示?

+0

澄清你想找到的最小值和最大值为每车/整个集合中的文档/对象?或整个集合中的一分钟和一个最大值 – ibininja

+0

到整个集合 – imperugo

+0

因此,根据您的示例数据,您希望得到类似这样的结果? '{min:20100.0,max:40250.0}' –

回答

1

这是你在找什么:用最小和最大整个集合在

> 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"} 
         } 
        } 
        ]) 

这将输出一个值。

+1

似乎很好! 谢谢,让我试着将它转换为C#,但运行在它的shell上运行 – imperugo

+0

不错,很高兴它有帮助。 – ibininja

+1

您可以通过在Price.PricesType上添加索引并在$ unwind之前添加另一个$匹配来优化此查询,以便仅保留数组中至少有一个具有此PriceType的价格的文档。展开之后仍然需要相同的$匹配,但是这个不会受益于索引。这将使$ unwind + $匹配更快,但它取决于您的数据集。如果100%的数据具有PriceType = 4,它将无济于事。 –

0

谁愿意知道如何(再次感谢@ibininja)转换为C#查询所有类似的人在这里的C#代码:

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();