2017-11-01 11 views
0

集計を使用して、コレクションの各年の最小日と最大日のレコードの特定の列の値の差に到達する方法パイプラインや地図のmongodbの削減?私は次の形式取るために結果を希望mongodbの各年の最小日と最大日の値の差に到達する方法

/* 1 */ 
{ 
    "_id" : 1, 
    "item" : "abc", 
    "price" : 10, 
    "quantity" : 2, 
    "date" : ISODate("2014-01-01T08:00:00.000Z") 
}, 

/* 2 */ 
{ 
    "_id" : 2, 
    "item" : "jkl", 
    "price" : 20, 
    "quantity" : 1, 
    "date" : ISODate("2014-02-03T09:00:00.000Z") 
}, 

/* 3 */ 
{ 
    "_id" : 3, 
    "item" : "xyz", 
    "price" : 5, 
    "quantity" : 5, 
    "date" : ISODate("2014-02-03T09:05:00.000Z") 
}, 

/* 4 */ 
{ 
    "_id" : 4, 
    "item" : "abc", 
    "price" : 10, 
    "quantity" : 10, 
    "date" : ISODate("2014-02-15T08:00:00.000Z") 
}, 

/* 5 */ 
{ 
    "_id" : 5, 
    "item" : "xyz", 
    "price" : 5, 
    "quantity" : 10, 
    "date" : ISODate("2014-02-15T09:05:00.000Z") 
}, 

/* 6 */ 
{ 
    "_id" : 6, 
    "item" : "abc", 
    "price" : 10, 
    "quantity" : 2, 
    "date" : ISODate("2013-01-01T08:00:00.000Z") 
}, 

/* 7 */ 
{ 
    "_id" : 7, 
    "item" : "jkl", 
    "price" : 20, 
    "quantity" : 1, 
    "date" : ISODate("2013-02-03T09:00:00.000Z") 
}, 

/* 8 */ 
{ 
    "_id" : 8, 
    "item" : "xyz", 
    "price" : 5, 
    "quantity" : 5, 
    "date" : ISODate("2013-02-03T09:05:00.000Z") 
}, 

/* 9 */ 
{ 
    "_id" : 9, 
    "item" : "abc", 
    "price" : 10, 
    "quantity" : 10, 
    "date" : ISODate("2013-02-15T08:00:00.000Z") 
}, 

/* 10 */ 
{ 
    "_id" : 10, 
    "item" : "xyz", 
    "price" : 5, 
    "quantity" : 10, 
    "date" : ISODate("2013-02-15T09:05:00.000Z") 
}, 

/* 11 */ 
{ 
    "_id" : 11, 
    "item" : "abc", 
    "price" : 10, 
    "quantity" : 2, 
    "date" : ISODate("2012-01-01T08:00:00.000Z") 
}, 

/* 12 */ 
{ 
    "_id" : 12, 
    "item" : "jkl", 
    "price" : 20, 
    "quantity" : 1, 
    "date" : ISODate("2012-02-03T09:00:00.000Z") 
}, 

/* 13 */ 
{ 
    "_id" : 13, 
    "item" : "xyz", 
    "price" : 5, 
    "quantity" : 5, 
    "date" : ISODate("2012-02-03T09:05:00.000Z") 
}, 

/* 14 */ 
{ 
    "_id" : 14, 
    "item" : "abc", 
    "price" : 10, 
    "quantity" : 10, 
    "date" : ISODate("2012-02-15T08:00:00.000Z") 
}, 

/* 15 */ 
{ 
    "_id" : 15, 
    "item" : "xyz", 
    "price" : 5, 
    "quantity" : 10, 
    "date" : ISODate("2012-02-15T09:05:00.000Z") 
}, 

私は、次のコレクション持っている各年の

{ 
{"year": 2014}, {"minDtQuantity": 2}, {"maxDtQuantity": 10}, {"quantityDiff": 8}, 
{"year": 2013}, {"minDtQuantity": 2}, {"maxDtQuantity": 10}, {"quantityDiff": 8}, 
{"year": 2012}, {"minDtQuantity": 2}, {"maxDtQuantity": 10}, {"quantityDiff": 8}, 
} 

を、私たちは、最小値と最大値の日付とグループを見つける必要がありますそれらの日付の「量」値を見つけて、各年の最小日と最大日の数量の差を求めます。

これはmongodbの集約パイプラインやmap-reduceでも可能ですか?

答えて

2

これは、集計パイプラインを日付でソートしてから、年単位でグループ化するときに配列にプッシュすることで実行できます($ year演算子を使用して日付オブジェクトから年を抽出します)。その年の最小日付と最大日付の数量は、配列の最初と最後の値です。これらは$ arrayElemAtを使って配列から取り出すことができます。

db.collection.aggregate(
[ 
    { 
     $sort: { 
      "date": 1 
     } 
    }, 
    { 
     $group: { 
      "_id": { "$year": "$date" }, 
      "quantityArray": { "$push": "$quantity" }, 
     } 
    }, 
    { 
     $project: { 
      "_id": 0, 
      "year": "$_id", 
      "minDtQuantity": { "$arrayElemAt": [ "$quantityArray", 0 ] }, 
      "maxDtQuantity": { "$arrayElemAt": [ { "$reverseArray": "$quantityArray" }, 0 ] }, 
      "quantityDiff": { "$subtract": [ 
           { "$arrayElemAt": [ { "$reverseArray": "$quantityArray" }, 0 ] }, 
           { "$arrayElemAt": [ "$quantityArray", 0 ] }, 
              ] } 
     } 
    }, 
    ] 
); 

この凝集は、あなたのデータにこれらの結果を返します。

{ 
"year" : NumberInt(2014), 
"minDtQuantity" : NumberInt(2), 
"maxDtQuantity" : NumberInt(10), 
"quantityDiff" : NumberInt(-8) 
}, 
{ 
"year" : NumberInt(2013), 
"minDtQuantity" : NumberInt(2), 
"maxDtQuantity" : NumberInt(10), 
"quantityDiff" : NumberInt(-8) 
}, 
{ 
"year" : NumberInt(2012), 
"minDtQuantity" : NumberInt(2), 
"maxDtQuantity" : NumberInt(10), 
"quantityDiff" : NumberInt(-8) 
} 

これは、指定したかなり形式ではありません。私はあなたが何を必要としているか正確にはわかりません。結果を1つの文書に戻す必要がありますか?

+0

ありがとう、サム!感謝します! –

関連する問題