聚合结果组对象

时间:2017-07-19 19:42:17

标签: mongodb mongodb-query aggregation-framework

我到达的地方是,我需要将用户数据放在athele上:

{ 
"_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)

}

运动员:

{ 
"_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)

}

我已经尝试与项目一起退出并设置了一个组,但我没有达到预期的结果。我想我在某些方面错过了。

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"}
]);

0 个答案:

没有答案