2016-02-12 39 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();