title: Aggregate result group objects
body: <p>Where I arrived was this I needed to put the user data on the athele:</p>
  
  <pre><code>{ 
  "_id" : ObjectId("5963c6aa1aaf2c1c702ea86f"), 
  "updatedAt" : ISODate("2017-07-10T18:25:46.941+0000"), 
  "createdAt" : ISODate("2017-07-10T18:25:46.933+0000"), 
  "name" : "athleta01", 
  "nickname" : "01", 
  "email" : "aaaa@terra.com.br", 
  "birthday" : ISODate("1986-09-15T03:00:00.000+0000"), 
  "gender" : "0", 
  "disable" : false, 
  "deletedAt" : false, 
  "profile" : [
      {
          "departament" : ObjectId("5963c6281aaf2c1c702ea86c"), 
          "profession" : "athlete", 
          "_id" : ObjectId("5963c6aa1aaf2c1c702ea870")
      }
  ], 
  "photo" : null, 
  "__v" : NumberInt(0)
  </code></pre>
  
  <p>}</p>
  
  <p>doc Athlete: </p>
  
  <pre><code>{ 
  "_id" : ObjectId("5963c6aa1aaf2c1c702ea871"), 
  "updatedAt" : ISODate("2017-07-10T19:53:08.285+0000"), 
  "createdAt" : ISODate("2017-07-10T18:25:46.948+0000"), 
  "position" : "atacante", 
  "shirt" : NumberInt(15), 
  "document" : "87956421345", 
  "weight" : "10.5", 
  "height" : "2.73", 
  "profileId" : ObjectId("5963c6aa1aaf2c1c702ea870"), 
  "disable" : false, 
  "deletedAt" : false, 
  "device" : {
      "activityLevel" : "vai atuazliar", 
      "maxHeartRate" : NumberInt(45), 
      "reposeHeartRate" : NumberInt(90), 
      "codSensor" : "1a2s4e1s"
  }, 
  "__v" : NumberInt(0)
  </code></pre>
  
  <p>}</p>
  
  <p>Already tried to make the exit with the project and to set up a group also I did not have the result that I expected. I think I'm missing out on some point.</p>
  
  <pre><code>db.users.aggregate(
  [
      {
        $match: {
          "profile.departament" : ObjectId("5963c6281aaf2c1c702ea86c")
        }
      },
  
      {
          $lookup: {
              "from" : "institution",
              "localField" : "profile.departament",
              "foreignField" : "departament._id",
              "as" : "user_departament"
          }
      },
      {
          $lookup: {
              "from" : "athlete",
              "localField" : "profile._id",
              "foreignField" : "profileId",
              "as" : "athlete_departament"
          }
      },
  
      {
          $unwind: {
          path: "$athlete_departament",
          preserveNullAndEmptyArrays: true
          }
      },
  
      {
          $lookup: {
          from: "users",
          localField: "athlete_departament.profileId",
          foreignField: "profile._id",
          as: "username",
          }
      },
  
      { 
          $group: {
            _id: { name: "$user_departament.name", cnpj: "$user_departament.cnpj", photo: "$user_departament.photo"},
            departaments: {
              $push : "$user_departament.departament"
              },
            athletes: {
              $push : '$athlete_departament',
            }
          }
      },
  
      {$unwind : "$departaments"}, 
      {$unwind : "$departaments"}, 
      {$unwind : "$_id.name"}, 
      {$unwind : "$_id.cnpj"}, 
      {$unwind : "$_id.photo"},
      {$unwind : "$athletes"}, 
      {$unwind : "$athletes"}, 
  
  
      {
        $group : {
          _id : "$_id",
          departaments : {$addToSet : "$departaments"},
          athletes : {$addToSet : "$athletes"}
        }
      },
  
      { "$project": {
          //_id : { name: "$_id", departaments: { $concatArrays: [ {$arrayElemAt: [ "$departaments", 0 ]}, "$profiles", "$athletes"  ] }}
          _id : { institution: "$_id", departaments: { info: "$departaments", profiles: "$profiles", athletes: { username: "$username", athlete: "$athletes" },  }}
          //_id : { name: "$_id", departaments: { "departament": { $concatArrays: [ { $arrayElemAt: ["$departaments", 0]}, "$athletes"  ] }}}
          //_id : { name: "$_id", departaments: { $concatArrays: [ {$arrayElemAt: [ "$departaments", 0 ]}, "$profiles", "$athletes"  ] }}
      }},
  
      {$unwind : "$_id.departaments.info"},
      {$unwind : "$_id.departaments.info"}
  ]);
  </code></pre>