Mongo Aggregation $group by _id and date

2.2k views Asked by At

I have a mongo aggregation query that is currently grouping results based on a location id, and using $push to return a count value using $size of elements in an array for the resulting documents matching a given date. I need to adjust the output for each result grouped by location id and $sum the count values grouped by date. Examples below.

The current result output resembles:

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

I'm attempting to produce the following output:

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

My current query:

[
    {  $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'
                }
            }
        }
    }
]

Solution: Adding the following query provided by @Rfroes87 to the end of the pipeline solved the issue.

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

There are 2 answers

4
turivishal On BEST ANSWER

Probably you can try, not sure with your document structure,

Predicted document structure:

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

Playground

0
varman On

You can try following

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

Working Mongo playground