1

I have an aggregation query which splits my data into buckets. I was wondering if there's an option to have several more aggregation pipeline phases which will run on each bucket separately.

Here's my data:

[
   {
      "entityId":1,
      "accountId":"X",
      "date":"2019-10-10"
   },
   {
      "entityId":2,
      "accountId":"X",
      "date":"2019-10-10"
   },
   {
      "entityId":3,
      "accountId":"Y",
      "date":"2019-10-10"
   },
   {
      "entityId":4,
      "accountId":"Y",
      "date":"2019-10-10"
   }
]

I am splitting my data to buckets of accountId, and now it looks like this -

[
   {
      "accountId":"X",
      "results":[
         {
            "entityId":1,
            "date":"2019-10-10"
         }
      ]
   },
   {
      "accountId":"Y",
      "results":[
         {
            "entityId":3,
            "date":"2019-10-10"
         }
      ]
   }
]

I would like to continue with the aggregation pipeline on each bucket separately, and group the rest of the data by "date", so it will look like this:

[
   {
      "accountId":"X",
      "results":[
         {
            "date":"2019-10-10",
            "results":[
               {
                  "entityId":1
               },
               {
                  "entityId":2
               }
            ]
         }
      ]
   },
   {
      "accountId":"Y",
      "results":[
         {
            "date":"2019-10-10",
            "results":[
               {
                  "entityId":3
               },
               {
                  "entityId":4
               }
            ]
         }
      ]
   }
]

I'm trying to understand how to use a $bucket step on the result of a previous $bucket, but I'm having trouble with it..

Thanks in advance!

styvane
  • 59,869
  • 19
  • 150
  • 156
Yehuda
  • 73
  • 7

0 Answers0