Mongo Aggregation $ group by _id and date

Aug 29 2020

現在、ロケーションIDに基づいて結果をグループ化し、を使用しているmongo集計クエリがあります $push to return a count value using $特定の日付に一致する結果のドキュメントの配列内の要素のサイズ。場所IDでグループ化された各結果の出力を調整し、日付でグループ化されたカウント値を$ sumする必要があります。以下の例。

現在の結果出力は次のようになります。

[{ 
    "_id" : "100", 
    "appts" : [
        {
            "count" : NumberInt(2), 
            "date" : "2020-08-07"
        }, 
        {
            "count" : NumberInt(2), 
            "date" : "2020-08-07"
        } ]
},
 { 
    "_id" : "103", 
    "appts" : [
        {
            "count" : NumberInt(1), 
            "date" : "2020-08-07"
        }, 
        {
            "count" : NumberInt(3), 
            "date" : "2020-08-07"
        },
        {
            "count" : NumberInt(2), 
            "date" : "2020-08-08"
        } ]
}]

次の出力を生成しようとしています。

[{ 
    "_id" : "100", 
    "appts" : [
        {
            "count" : NumberInt(4), 
            "date" : "2020-08-07"
        } ]
},
 { 
    "_id" : "103", 
    "appts" :  [
        {
            "count" : NumberInt(4), 
            "date" : "2020-08-07"
        }, 
        {
            "count" : NumberInt(2), 
            "date" : "2020-08-08"
        } ]
}]

私の現在のクエリ:

[
    {  $match: { ... } },
    {
        $group: { _id: { date: { $dateToString: { format: '%Y-%m-%d', date: '$time' } }, loc: '$location.branchId',
                additionalReminders: '$analytics.twilio.additionalReminders' } } }, { $group: {
            _id: '$_id.loc', appts: { $push: {
                    count: { $size: '$_id.additionalReminders' },
                    date: '$_id.date'
                }
            }
        }
    }
]

解決策:@ Rfroes87によって提供される次のクエリをパイプラインの最後に追加すると、問題が解決しました。

{ $unwind: "$appts" }, { $group: {
        _id: { "id": "$_id", "date": "$appts.date" },
        "count": { $sum: "$appts.count" }
    }
},
{
    $group: { _id: "$_id.id",
        "appts": {  $push: { "count": "$count", "date": "$_id.date" }  }
    } 
}

回答

3 turivishal Aug 29 2020 at 01:01

おそらく、ドキュメントの構造がわからない場合でも、試すことができます。

予測されるドキュメント構造:

[{"time":ISODate("1970-01-01T00:00:00Z"),"location":{branchId:1},analytics:{twilio:{additionalReminders:[1,2,3]}}},{"time":ISODate("1970-01-01T00:00:00Z"),"location":{branchId:1},analytics:{twilio:{additionalReminders:[1,2]}}},{"time":ISODate("1970-01-02T00:00:00Z"),"location":{branchId:1},analytics:{twilio:{additionalReminders:[1,2,3]}}},]
db.collection.aggregate([
  {
    $group: { _id: { date: { $dateToString: { format: "%Y-%m-%d", date: "$time" } }, loc: "$location.branchId"
      },
      // get size of array and sum here
      additionalReminders: {
        $sum: { $size: "$analytics.twilio.additionalReminders" } } } }, { $group: {
      _id: "$_id.loc", appts: { $push: {
          // add just field here
          count: "$additionalReminders", date: "$_id.date"
        }
      }
    }
  }
])

遊び場

2 varman Aug 29 2020 at 00:54

フォローしてみてください

db.collection.aggregate([
  {
    $unwind: "$appts"
  },
  {
    $group: { _id: { "_id": "$_id",
        "date": "$appts.date" }, "count": { $sum: "$appts.count" } } }, { $group: {
      _id: "$_id._id", "appts": { "$addToSet": {
          "count": "$count", "date": "$_id.date"
        }
      }
    }
  }
])

働くモンゴの遊び場