Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Aggregate data in same collection using name

I am new to mongo please help me to merge the different data sets with the same block and floor name into one dataset, Actually, the below format is in one database collection with 4 data-set.
can anyone help me to solve this problem in nodejs and mongo?
I tried many ways to solve this issue but I didn’t get any proper output, please help me as soon as possible, thanks in advance

[{
BlockName:"B1",
FloorName:"F1"
Units:[{
       UnitName:104,
       Location:["Hall","Living"]
     }]
},

{
BlockName:"B1",
FloorName:"F2"
Units:[{
        UnitName:104,
        Location:["Hall","Living"]
     }]
},

{
BlockName:"B2",
FloorName:"F1"
Units:[{
       UnitName:104,
       Location:["Hall","Living"]
     }]
},

{
BlockName:"B2",
FloorName:"F2"
Units:[{
       UnitName:104,
       Location:["Hall","Living"]
     }]
}]

I need an output structure like this below:-

[{
BlockName:"B1",
Floors:[
    {
        FloorName:"F1"
        Units:[{
            UnitName:104,
            Location:["Hall","Living"]
        }]
    },
    {
        FloorName:"F2"
        Units:[{
            UnitName:104,
            Location:["Hall","Living"]
        }]
    }
]
},
{
BlockName:"B2",
Floors:[
    {
        FloorName:"F1"
        Units:[{
            UnitName:104,
            Location:["Hall","Living"]
        }]
    },
    {
        FloorName:"F2"
        Units:[{
            UnitName:104,
            Location:["Hall","Living"]
        }]
    }
]
}]

I am new to mongo please help me to merge the different data set with the same block and floor name into one dataset, Actually, the below format is in one database collection with 4 data-set
can anyone help me to solve this problem in nodejs and mongo
I tried many way to solve this issue but I didn't get any proper output, please help me as soon as possible, thanks in advance 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  1. $group – Group by BlockName and push the documents into Floors array.
  2. $project – Decorate output documents.
db.collection.aggregate([
  {
    $group: {
      _id: "$BlockName",
      Floors: {
        $push: {
          "FloorName": "$FloorName",
          "Units": "$Units"
        }
      }
    }
  },
  {
    $project: {
      _id: 0,
      BlockName: "$_id",
      Floors: 1
    }
  }
])

Sample Mongo Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading