How to create an aggregate based on unique elements as fields?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I am trying to aggregate documents based on diagnosisType, and at a higher level patientKey and encounterKey. This is what the documents look like:



{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "medical_history",
"diagnosisCode" : "Z87.81",
}

{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "problem_list",
"diagnosisCode" : "2x.2",
}

{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "D21.01",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "X2.31",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "problem_list",
"diagnosisCode" : "p.2342",
}


This is what I would like the aggregate to look like:



{
"patientKey": 1,
"encounters":[{
encounterKey: 2,
medical_history: [Z87.81],
problem_list: [2x.2]
},
{
encounterKey: 3,
medical_history: [D21.01, X2.31],
problem_list: [p.2342]
}]
}


Any ideas how I could approach this?



I have tried the following for medical history and then problem list, my initial thought is to first aggregate on Problem List and then on Medical History, then combine the collections, afterwards do another aggregate based on encounters, and then finally another aggregate on patientid.



But it's a problem combining heterogeous documents.



db.collections.aggregate([
{$match:{diagnosisType:"Problem List"}},
{$group:{_id:"$encounterKey",
"ProblemList": {$push:{$concat:"$diagnosisCode"}},
"durableKey":{$first:"$durableKey"}}},
{$project:{_id:0,
encounterKey:"$_id",
ProblemList:1,
durableKey:1}},
{$out:"output"}
],{
allowDiskUse: true
})









share|improve this question































    2















    I am trying to aggregate documents based on diagnosisType, and at a higher level patientKey and encounterKey. This is what the documents look like:



    {
    "_id" : ObjectId("...."),
    "patientKey" : 1,
    "encounterKey" : 2,
    "diagnosisType" : "medical_history",
    "diagnosisCode" : "Z87.81",
    }

    {
    "_id" : ObjectId("...."),
    "patientKey" : 1,
    "encounterKey" : 2,
    "diagnosisType" : "problem_list",
    "diagnosisCode" : "2x.2",
    }

    {
    "_id" : ObjectId("...."),
    "patientKey" : 1,
    "encounterKey" : 3,
    "diagnosisType" : "medical_history",
    "diagnosisCode" : "D21.01",
    }
    {
    "_id" : ObjectId("...."),
    "patientKey" : 1,
    "encounterKey" : 3,
    "diagnosisType" : "medical_history",
    "diagnosisCode" : "X2.31",
    }
    {
    "_id" : ObjectId("...."),
    "patientKey" : 1,
    "encounterKey" : 3,
    "diagnosisType" : "problem_list",
    "diagnosisCode" : "p.2342",
    }


    This is what I would like the aggregate to look like:



    {
    "patientKey": 1,
    "encounters":[{
    encounterKey: 2,
    medical_history: [Z87.81],
    problem_list: [2x.2]
    },
    {
    encounterKey: 3,
    medical_history: [D21.01, X2.31],
    problem_list: [p.2342]
    }]
    }


    Any ideas how I could approach this?



    I have tried the following for medical history and then problem list, my initial thought is to first aggregate on Problem List and then on Medical History, then combine the collections, afterwards do another aggregate based on encounters, and then finally another aggregate on patientid.



    But it's a problem combining heterogeous documents.



    db.collections.aggregate([
    {$match:{diagnosisType:"Problem List"}},
    {$group:{_id:"$encounterKey",
    "ProblemList": {$push:{$concat:"$diagnosisCode"}},
    "durableKey":{$first:"$durableKey"}}},
    {$project:{_id:0,
    encounterKey:"$_id",
    ProblemList:1,
    durableKey:1}},
    {$out:"output"}
    ],{
    allowDiskUse: true
    })









    share|improve this question



























      2












      2








      2








      I am trying to aggregate documents based on diagnosisType, and at a higher level patientKey and encounterKey. This is what the documents look like:



      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 2,
      "diagnosisType" : "medical_history",
      "diagnosisCode" : "Z87.81",
      }

      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 2,
      "diagnosisType" : "problem_list",
      "diagnosisCode" : "2x.2",
      }

      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 3,
      "diagnosisType" : "medical_history",
      "diagnosisCode" : "D21.01",
      }
      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 3,
      "diagnosisType" : "medical_history",
      "diagnosisCode" : "X2.31",
      }
      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 3,
      "diagnosisType" : "problem_list",
      "diagnosisCode" : "p.2342",
      }


      This is what I would like the aggregate to look like:



      {
      "patientKey": 1,
      "encounters":[{
      encounterKey: 2,
      medical_history: [Z87.81],
      problem_list: [2x.2]
      },
      {
      encounterKey: 3,
      medical_history: [D21.01, X2.31],
      problem_list: [p.2342]
      }]
      }


      Any ideas how I could approach this?



      I have tried the following for medical history and then problem list, my initial thought is to first aggregate on Problem List and then on Medical History, then combine the collections, afterwards do another aggregate based on encounters, and then finally another aggregate on patientid.



      But it's a problem combining heterogeous documents.



      db.collections.aggregate([
      {$match:{diagnosisType:"Problem List"}},
      {$group:{_id:"$encounterKey",
      "ProblemList": {$push:{$concat:"$diagnosisCode"}},
      "durableKey":{$first:"$durableKey"}}},
      {$project:{_id:0,
      encounterKey:"$_id",
      ProblemList:1,
      durableKey:1}},
      {$out:"output"}
      ],{
      allowDiskUse: true
      })









      share|improve this question
















      I am trying to aggregate documents based on diagnosisType, and at a higher level patientKey and encounterKey. This is what the documents look like:



      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 2,
      "diagnosisType" : "medical_history",
      "diagnosisCode" : "Z87.81",
      }

      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 2,
      "diagnosisType" : "problem_list",
      "diagnosisCode" : "2x.2",
      }

      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 3,
      "diagnosisType" : "medical_history",
      "diagnosisCode" : "D21.01",
      }
      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 3,
      "diagnosisType" : "medical_history",
      "diagnosisCode" : "X2.31",
      }
      {
      "_id" : ObjectId("...."),
      "patientKey" : 1,
      "encounterKey" : 3,
      "diagnosisType" : "problem_list",
      "diagnosisCode" : "p.2342",
      }


      This is what I would like the aggregate to look like:



      {
      "patientKey": 1,
      "encounters":[{
      encounterKey: 2,
      medical_history: [Z87.81],
      problem_list: [2x.2]
      },
      {
      encounterKey: 3,
      medical_history: [D21.01, X2.31],
      problem_list: [p.2342]
      }]
      }


      Any ideas how I could approach this?



      I have tried the following for medical history and then problem list, my initial thought is to first aggregate on Problem List and then on Medical History, then combine the collections, afterwards do another aggregate based on encounters, and then finally another aggregate on patientid.



      But it's a problem combining heterogeous documents.



      db.collections.aggregate([
      {$match:{diagnosisType:"Problem List"}},
      {$group:{_id:"$encounterKey",
      "ProblemList": {$push:{$concat:"$diagnosisCode"}},
      "durableKey":{$first:"$durableKey"}}},
      {$project:{_id:0,
      encounterKey:"$_id",
      ProblemList:1,
      durableKey:1}},
      {$out:"output"}
      ],{
      allowDiskUse: true
      })






      mongodb aggregation-framework






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 17:20









      mickl

      16.3k61741




      16.3k61741










      asked Jan 4 at 16:56









      user252046user252046

      6318




      6318
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can use below aggregation:



          db.col.aggregate([
          {
          $group: {
          _id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
          problem_list: { $push: "$diagnosisCode" }
          }
          },
          {
          $group: {
          _id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
          encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
          }
          },
          {
          $project: {
          encounters: { $arrayToObject: "$encounters" }
          }
          },
          {
          $group: {
          _id: "$_id.patientKey",
          encounters: {
          $push: {
          encounterKey: "$_id.encounterKey",
          problem_list: "$encounters.problem_list",
          medical_history: "$encounters.medical_history"
          }
          }
          }
          },
          {
          $project: {
          patientKey: "$_id",
          _id: 0,
          encounters: 1
          }
          }
          ])


          Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.



          Outputs:



          {
          "patientKey" : 1,
          "encounters" : [
          {
          "encounterKey" : 2,
          "problem_list" : [
          "2x.2"
          ],
          "medical_history" : [
          "Z87.81"
          ]
          },
          {
          "encounterKey" : 3,
          "problem_list" : [
          "p.2342"
          ],
          "medical_history" : [
          "D21.01",
          "X2.31"
          ]
          }
          ]
          }





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043124%2fhow-to-create-an-aggregate-based-on-unique-elements-as-fields%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can use below aggregation:



            db.col.aggregate([
            {
            $group: {
            _id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
            problem_list: { $push: "$diagnosisCode" }
            }
            },
            {
            $group: {
            _id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
            encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
            }
            },
            {
            $project: {
            encounters: { $arrayToObject: "$encounters" }
            }
            },
            {
            $group: {
            _id: "$_id.patientKey",
            encounters: {
            $push: {
            encounterKey: "$_id.encounterKey",
            problem_list: "$encounters.problem_list",
            medical_history: "$encounters.medical_history"
            }
            }
            }
            },
            {
            $project: {
            patientKey: "$_id",
            _id: 0,
            encounters: 1
            }
            }
            ])


            Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.



            Outputs:



            {
            "patientKey" : 1,
            "encounters" : [
            {
            "encounterKey" : 2,
            "problem_list" : [
            "2x.2"
            ],
            "medical_history" : [
            "Z87.81"
            ]
            },
            {
            "encounterKey" : 3,
            "problem_list" : [
            "p.2342"
            ],
            "medical_history" : [
            "D21.01",
            "X2.31"
            ]
            }
            ]
            }





            share|improve this answer




























              1














              You can use below aggregation:



              db.col.aggregate([
              {
              $group: {
              _id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
              problem_list: { $push: "$diagnosisCode" }
              }
              },
              {
              $group: {
              _id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
              encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
              }
              },
              {
              $project: {
              encounters: { $arrayToObject: "$encounters" }
              }
              },
              {
              $group: {
              _id: "$_id.patientKey",
              encounters: {
              $push: {
              encounterKey: "$_id.encounterKey",
              problem_list: "$encounters.problem_list",
              medical_history: "$encounters.medical_history"
              }
              }
              }
              },
              {
              $project: {
              patientKey: "$_id",
              _id: 0,
              encounters: 1
              }
              }
              ])


              Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.



              Outputs:



              {
              "patientKey" : 1,
              "encounters" : [
              {
              "encounterKey" : 2,
              "problem_list" : [
              "2x.2"
              ],
              "medical_history" : [
              "Z87.81"
              ]
              },
              {
              "encounterKey" : 3,
              "problem_list" : [
              "p.2342"
              ],
              "medical_history" : [
              "D21.01",
              "X2.31"
              ]
              }
              ]
              }





              share|improve this answer


























                1












                1








                1







                You can use below aggregation:



                db.col.aggregate([
                {
                $group: {
                _id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
                problem_list: { $push: "$diagnosisCode" }
                }
                },
                {
                $group: {
                _id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
                encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
                }
                },
                {
                $project: {
                encounters: { $arrayToObject: "$encounters" }
                }
                },
                {
                $group: {
                _id: "$_id.patientKey",
                encounters: {
                $push: {
                encounterKey: "$_id.encounterKey",
                problem_list: "$encounters.problem_list",
                medical_history: "$encounters.medical_history"
                }
                }
                }
                },
                {
                $project: {
                patientKey: "$_id",
                _id: 0,
                encounters: 1
                }
                }
                ])


                Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.



                Outputs:



                {
                "patientKey" : 1,
                "encounters" : [
                {
                "encounterKey" : 2,
                "problem_list" : [
                "2x.2"
                ],
                "medical_history" : [
                "Z87.81"
                ]
                },
                {
                "encounterKey" : 3,
                "problem_list" : [
                "p.2342"
                ],
                "medical_history" : [
                "D21.01",
                "X2.31"
                ]
                }
                ]
                }





                share|improve this answer













                You can use below aggregation:



                db.col.aggregate([
                {
                $group: {
                _id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
                problem_list: { $push: "$diagnosisCode" }
                }
                },
                {
                $group: {
                _id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
                encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
                }
                },
                {
                $project: {
                encounters: { $arrayToObject: "$encounters" }
                }
                },
                {
                $group: {
                _id: "$_id.patientKey",
                encounters: {
                $push: {
                encounterKey: "$_id.encounterKey",
                problem_list: "$encounters.problem_list",
                medical_history: "$encounters.medical_history"
                }
                }
                }
                },
                {
                $project: {
                patientKey: "$_id",
                _id: 0,
                encounters: 1
                }
                }
                ])


                Basically you need few $group stages to accumulate the data. Additionally you have to use $arrayToObject to build your JSON keys dynamically.



                Outputs:



                {
                "patientKey" : 1,
                "encounters" : [
                {
                "encounterKey" : 2,
                "problem_list" : [
                "2x.2"
                ],
                "medical_history" : [
                "Z87.81"
                ]
                },
                {
                "encounterKey" : 3,
                "problem_list" : [
                "p.2342"
                ],
                "medical_history" : [
                "D21.01",
                "X2.31"
                ]
                }
                ]
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 4 at 17:14









                micklmickl

                16.3k61741




                16.3k61741
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043124%2fhow-to-create-an-aggregate-based-on-unique-elements-as-fields%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas