同一对象的Mongodb嵌套组

时间:2019-05-24 23:15:08

标签: mongodb mongodb-query aggregation-framework aggregate-functions

我有一个这样的对象数组:

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

我正在尝试按category分组并创建一个名为children的数组,然后该数组具有另一个嵌套的serviceClassification分组的子项数组

所需的输出

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

我已经尝试了几个小时,这是我尝试过的代码示例:

https://mongoplayground.net/p/Asb5THvqlf6

1 个答案:

答案 0 :(得分:1)

我首先将其按类别分组,然后按serviceClassification分组,剩下的转换工作留给您。

https://mongoplayground.net/p/zOIt2Q45HlJ