Mongo Aggregation $ group by _id 및 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"
        }
      }
    }
  }
])

일하는 몽고 놀이터