将聚合中的字段值添加到数组

时间:2018-10-03 18:09:29

标签: mongodb mongodb-query aggregation-framework aggregate

技能收集:

    "skillID" : "10cff2ae-76e7-455a-b1f2-07c6104d254b",
    "skillname" : "java",
    "languageID" : "84c2613c-81c3-4477-ab38-433aaef74373",
    "languagename" : "English",
    "noofendorsement" : 0

    "skillID" : "10cff2ae-76e7-455a-b1f2-07c6104d254b",
    "skillname" : "जावा",
    "languageID" : "hindi",
    "languagename" : "hindi",
    "noofendorsement" : 0



"skillID" : "39bca1af-bea4-4a24-9f50-33992f8f8a6e",
    "skillname" : "python",
    "languageID" : "84c2613c-81c3-4477-ab38-433aaef74373",
    "languagename" : "English",
    "noofendorsement" : 0

"skillID" : "39bca1af-bea4-4a24-9f50-33992f8f8a6e",
    "skillname" : "अजगर",
    "languageID" : "hindi",
    "languagename" : "hindi",
    "noofendorsement" : 0

工作集合:

    "_id" : ObjectId("5bace08ce4b022aa88870ab2"),
    "_class" : "com.citizenchat.model.Jobs",
    "userID" : "ff7f88d9-d0e5-4c10-a7b0-b9fd8b5e89eb",
    "jobID" : "4d0b02b9-c508-42cb-a2c1-417f6b3d422b",
    "jobProfile" : "Legislators",
    "lastDate" : "29-09-2018",
    "jobEndDateMilliSeconds" : NumberLong(1538245799999),
    "jobEndDate" : ISODate("2018-09-29T18:29:59.999Z"),
    "jobstatus" : "Expired",
    "jobType" : "Fulltime",
    "jobDescription" : "Job",
    "postingDate" : "27-09-2018 13:52:12.787",
    "user_Skilllist" : [ 
        "10cff2ae-76e7-455a-b1f2-07c6104d254b"
    ],
    "user_occupationname" : "Legislators",
    "user_occupationid" : "154bddd1-5224-47cf-b3d0-f7dfc3ae9c83",
    "jobPostDateTimeMilliseconds" : NumberLong(1538850599999),

我正在使用以下聚合查询。

db.Jobs.aggregate([
{"$match":{"jobID":"4d0b02b9-c508-42cb-a2c1-417f6b3d422b"}},
 {
    $lookup:
       {
          from: "Skill",
          let:{"user_Skilllist":"$user_Skilllist"},
           pipeline:[
              {"$match":
                 {"$or":[
                    {"languageID":"hindi","$expr":{"$in":["$skillID","$$user_Skilllist"]}},
                    {"languageID":"84c2613c-81c3-4477-ab38-433aaef74373","$expr":{"$in":["$skillID","$$user_Skilllist"]}}
                         ]

                    }

               },

          ],
          as: "skills"
      }
 },



])

我得到以下结果。但是我的要求是 1)我将通过languageID仅获取该语言的技能。如果我不喜欢的语言没有该技能,则需要从默认语言英语获取。但是当我通过语言ID时,相同的技能存在于不同语言中我正在使用的语言。

2)从聚合中获取所有技能后,将所有技能名称传递给数组。

 {
    "_id" : ObjectId("5bace08ce4b022aa88870ab2"),
    "_class" : "com.citizenchat.model.Jobs",
    "userID" : "ff7f88d9-d0e5-4c10-a7b0-b9fd8b5e89eb",
    "jobID" : "4d0b02b9-c508-42cb-a2c1-417f6b3d422b",
    "jobProfile" : "Legislators",
    "lastDate" : "29-09-2018",
    "jobEndDateMilliSeconds" : NumberLong(1538245799999),
    "jobEndDate" : ISODate("2018-09-29T18:29:59.999Z"),
    "jobstatus" : "Expired",
    "jobType" : "Fulltime",
    "jobDescription" : "Job",
    "postingDate" : "27-09-2018 13:52:12.787",
    "user_Skilllist" : [ 
        "10cff2ae-76e7-455a-b1f2-07c6104d254b"
    ],
    "user_occupationname" : "Legislators",
    "user_occupationid" : "154bddd1-5224-47cf-b3d0-f7dfc3ae9c83",
    "jobPostDateTimeMilliseconds" : NumberLong(1538850599999),

    "skills" : [ 
        {
            "_id" : ObjectId("5ba0fbc7e4b03e2c8b8f6519"),
            "_class" : "com.citizenchat.model.Skill",
            "skillID" : "10cff2ae-76e7-455a-b1f2-07c6104d254b",
            "skillname" : "java",
            "languageID" : "84c2613c-81c3-4477-ab38-433aaef74373",
            "languagename" : "English",
            "noofendorsement" : 0
        }, 
        {
            "_id" : ObjectId("5bb47d07fc7ab61be62de768"),
            "skillID" : "10cff2ae-76e7-455a-b1f2-07c6104d254b",
            "skillname" : "जावा",
            "languageID" : "hindi",
            "languagename" : "hindi",
            "noofendorsement" : 0
        }
    ]
}

我希望输出应该是这样

"userID" : "ff7f88d9-d0e5-4c10-a7b0-b9fd8b5e89eb",
    "jobID" : "4d0b02b9-c508-42cb-a2c1-417f6b3d422b",
    "jobProfile" : "Legislators",
    "lastDate" : "29-09-2018",
    "jobEndDateMilliSeconds" : NumberLong(1538245799999),
    "jobEndDate" : ISODate("2018-09-29T18:29:59.999Z"),
    "jobstatus" : "Expired",
    "jobType" : "Fulltime",
    "jobDescription" : "Job",
    "postingDate" : "27-09-2018 13:52:12.787",
    "user_Skilllist" : [ 
        "10cff2ae-76e7-455a-b1f2-07c6104d254b"
    ],
    "user_occupationname" : "Legislators",
    "user_occupationid" : "154bddd1-5224-47cf-b3d0-f7dfc3ae9c83",
    "jobPostDateTimeMilliseconds" : NumberLong(1538850599999),

    "skills" : [ 
        "जावा",
         "अजगर"
    ]
}

1 个答案:

答案 0 :(得分:0)

以下是使用$facet的方法:

db.Jobs.aggregate([{
    $match: { "jobID": "4d0b02b9-c508-42cb-a2c1-417f6b3d422b" }
}, {
    $lookup: {
        "from": "Skill",
        "let": { "user_Skilllist": "$user_Skilllist" },
        "pipeline": [{
                $match: { "$expr": { "$in": ["$skillID", "$$user_Skilllist"] } }
            }, {
                $facet: {
                    "perfectMatch": [ { $match: { "languageID": "hindi" } } ],
                    "fallBack": [ { $match: { "languageID": "84c2613c-81c3-4477-ab38-433aaef74373" } } ],
                }
            }, {
                $project: {
                    "skillname": { $arrayElemAt: [ { $concatArrays: [ "$perfectMatch.skillname", "$fallBack.skillname" ] }, 0 ] } // take the first proper value
                }
            }
        ],
        "as": "skills"
    }
}, {
    $addFields: { "skills": "$skills.skillname" }
}])

这是一个使用$switch的方法-的想法是将首选值映射到1,将后备值映射到2,将所有其余值映射到3,以便我们进行排序:

db.Jobs.aggregate([{
    $match: { "jobID": "4d0b02b9-c508-42cb-a2c1-417f6b3d422b" }
}, {
    $lookup: {
        "from": "Skill",
        "let": { "user_Skilllist": "$user_Skilllist" },
        "pipeline": [{
                $match: { "$expr": { "$in": ["$skillID", "$$user_Skilllist"] } }
            }, {
                $addFields: {
                    "preferredOrder": {
                        $switch: {
                           "branches": [
                              { "case": { $eq: [ "$languageID", "hindi" ] }, "then": 1 },
                              { "case": { $eq: [ "$languageID", "84c2613c-81c3-4477-ab38-433aaef74373" ] }, "then": 2 }
                           ],
                           "default": 3
                        }
                    }
                }
            }, {
                $sort: { "preferredOrder": 1 }
            }
        ],
        "as": "skills"
    }
}, {
    $addFields: {
        "skills": [{
            $arrayElemAt: [ "$skills.skillname", 0 ]
        }]
    }
}])

根据您的评论进行更新:

基于$switch的版本适用于user_Skilllist数组中的多个条目,如下所示:

db.Jobs.aggregate([{
    $match: { "jobID": "4d0b02b9-c508-42cb-a2c1-417f6b3d422b" }
}, {
    $lookup: {
        "from": "Skill",
        "let": { "user_Skilllist": "$user_Skilllist" },
        "pipeline": [{
                $match: { "$expr": { "$in": ["$skillID", "$$user_Skilllist"] } }
            }, {
                $addFields: {
                    "preferredOrder": {
                        $switch: {
                           "branches": [
                              { "case": { $eq: [ "$languageID", "hindi" ] }, "then": 1 },
                              { "case": { $eq: [ "$languageID", "84c2613c-81c3-4477-ab38-433aaef74373" ] }, "then": 2 }
                           ],
                           "default": 3
                        }
                    }
                }
            }, {
                $sort: { "preferredOrder": 1 }
            }, {
                $group: {
                    _id: "$skillID",
                    languages: { $first: "$skillname" }
                }
            }
        ],
        "as": "skills"
    }
}, {
    $addFields: {
        "skills": "$skills.languages"
    }
}])