0

I have an array of objects like so:

  {
    "sCode": "1010",
    "PCode": "1011",
    "category": "Diagnostic",
    "procedure": "Oral assessment for patients up to the age of 3 years inclusive. ",
    "serviceClassification": "First Dental Visit/Orientation",
  },

I'm trying to group by category and create an array called children, then this array has another nested array of children grouped by serviceClassification

Desired Output

[
  {
    "id": "Diagnostic",
    "text": "Diagnostic",
    "children": [
      {
        "id": "FIRST DENTAL VISIT/ORIENTATION",
        "text": "FIRST DENTAL VISIT/ORIENTATION",
        "children": [
          {
            "id": 0,
            "text": "01011 - Oral assessment for patients up to the age of 3 years inclusive."
          }
        ]
      }

I have been trying for couple of hours, here is a code sample of what I have tried:

https://mongoplayground.net/p/Asb5THvqlf6

Deano
  • 11,582
  • 18
  • 69
  • 119

1 Answers1

1

I got it to group by category first and then group by serviceClassification, I'll leave the rest of the transformation to you.

https://mongoplayground.net/p/zOIt2Q45HlJ

Plancke
  • 1,119
  • 11
  • 12