T3db0t
T3db0t

Reputation: 3551

Mongo aggregation: partitioning values into groups (by partition)

Using this approach, I can group a set of event documents by time. This solution returns the same input documents but with a partition number added.

How can I do the same thing but return the partitions instead? An example output document would be:

{
  partition: 0,
  startDate: ISODate("1900-04-12T18:30:00.000Z"),
  endDate: ISODate("2019-04-12T18:30:00.000Z"),
  numEvents: 27
}

Upvotes: 1

Views: 818

Answers (1)

mickl
mickl

Reputation: 49985

Let's say this is the current output of your aggregation:

{
    "_id" : null,
    "datesWithPartitions" : [
        {
            "date" : ISODate("2019-04-12T18:30:00Z"),
            "partition" : 0
        },
        {
            "date" : ISODate("2019-04-12T20:00:00Z"),
            "partition" : 1
        },
        {
            "date" : ISODate("2019-04-12T20:10:00Z"),
            "partition" : 1
        },
        {
            "date" : ISODate("2019-04-12T21:00:00Z"),
            "partition" : 2
        },
        {
            "date" : ISODate("2019-04-12T21:15:00Z"),
            "partition" : 2
        },
        {
            "date" : ISODate("2019-04-12T21:45:00Z"),
            "partition" : 3
        },
        {
            "date" : ISODate("2019-04-12T23:00:00Z"),
            "partition" : 4
        }
    ]
}

To get the data in a format you need you need to append following aggregation steps:

db.col.aggregate([
    {
        $unwind: "$datesWithPartitions"
    },
    {
        $group: {
            _id: "$datesWithPartitions.partition",
            numEvents: { $sum: 1 },
            startDate: { $min: "$datesWithPartitions.date" },
            endDate: { $max: "$datesWithPartitions.date" }
        }
    },
    {
        $project: {
            _id: 0,
            partition: "$_id",
            startDate: 1,
            endDate: 1,
            numEvents: 1
        }
    }
])

$unwind will return single date per document and then you can apply $group with $min and $max to get partition boundaries and $sum to count partition elements

Upvotes: 1

Related Questions