mongodb node.js嵌套数组字段串联单个变量结果

时间:2019-01-01 16:30:27

标签: arrays json mongodb variables

我正在模型聚合中使用此代码

{$project:{
    //for brevity
}},
{$project:{
    "employe_detail":{
        "$map":{
            "input":"$employe_detail",
            "as":"names",
            "in":{
                "name":{"$concat":["$$names.first_name",
                    " ","$$names.other_names"," ",
                    "$$names.last_name"]}
            }
        }
    }
}}

这是结果

{ "employe_detail" : [ { "name" : "Brian Smith" } ] }
{ "employe_detail" : [ { "name" : "Josh Clefton" } ] }
{ "employe_detail" : [ { "name" : "Treasure Dwayne" } ] }

当我尝试扩展结果字段输出时

{$project:{
    //for brevity
}},
{$project:{
    "employe_detail":{
        "$map":{
            "input":"$employe_detail",
            "as":"names",
            "in":{
                "name":{"$concat":["$$names.first_name",
                    " ","$$names.other_names"," ",
                    "$$names.last_name"]},
                "dept":"$$names.activity_year.activity_detail.dept",
                "time_spent": "$$names.activity_year.activity_detail.activity_time_spent.time_spent",
                "shift": "$$names.activity_year.activity_detail.activity_time_spent.shift"
            }
        }
    }
}}

结果是

{ "employe_detail" : [ { "name" : "Brian Smith",     "dept" : [ [ "spray", "smoothing", "assembling", "packaging" ] ],"time_spent" : [ [ [ 6 ], [ 15 ], [ 7 ], [ 8 ]] ], "shift" : [ [ [ "afternoon" ], [ "afternoon" ], [ "morning" ], [ "morning" ] ] ]} ] }
{ "employe_detail" : [ { "name" : "Josh Clefton",    "dept" : [ [ "spray", "shining", "shaping", "smoothing"] ],"time_spent" : [ [ [ 5 ], [ ], [ 8 ], [ 10 ] ] ], "shift" : [ [ [ "afternoon" ], [ ], [ "night" ], [ "night" ] ] ] } ] }
{ "employe_detail" : [ { "name" : "Treasure Dwayne", "dept" : [ [ "spray", "shaping", "smoothing", "assembling" ] ],    "time_spent" : [ [ [ 3 ], [ 9 ], [ 13 ], [ 9 ] ] ], "shift" : [ [ [ "morning" ], [ "morning" ], [ "morning" ], [ "morning" ] ] ] } ] }

请(至少)如何获得与此类似的内容

{ "employe_detail" : [ { "name" : "Brian Smith", 
        {"dept" :  "spray", "time_spent":6, "shift": "afternoon"},
        {"dept" :  "smoothing", "time_spent": 15 , "shift": "afternoon"},
        {"dept" :  "assembling", "time_spent": 7, "shift": "morning"},
        {"dept" :  "packaging", "time_spent": 8, "shift": "morning"}
        }]}
{ "employe_detail" : [ { "name" : "Josh Clefton", 
        {"dept" :  "spray", "time_spent":5, "shift": "afternoon"},
        {"dept" :  "shining", "time_spent":  , "shift": },
        {"dept" :  "shaping", "time_spent": 8, "shift": "night"},
        {"dept" :  "smoothing", "time_spent": 10, "shift": "night"}
        }]}

优选地,我想获取一个数据成为变量名,例如 而不是time_spent作为变量名,应该是dept的数据作为变量名

{ "employe_detail" : [ { "name" : "Josh Clefton", 
        {"spray":5, "shift": "afternoon"},
        {"shining":  , "shift": },
        {"shaping": 8, "shift": "night"},
        {"smoothing": 10, "shift": "night"}
        }]}

请我如何实现以上目标

更新 这是示例数据

{ "_id" : ObjectId("5bd548380a84d90b5c2bb416"), "details" : {"first_name" : "Brian", "other_names" : "Stone", "last_name" : "Smith", "gender" : "male", "date_of_birth" : ISODate("2009-03-05T00:00:00Z") }, "contact" : [ { "_id" : ObjectId("5bd548380a84d90b5c2bb417"), "residential_address" : "no 5 smith"} ], "health" : [ { "_id" : ObjectId("5bd548380a84d90b5c2bb419"), "illness_name" : "Cold", "sypmtom" : "Sneezing", "normal_treatment" : ""} ], "activity_year" : { "_id" : ObjectId("5bd548380a84d90b5c2bb41a"), "activity_detail" : [ { "_id" : ObjectId("5bd548380a84d90b5c2bb41b"), "dept" : "spray", "activity_time_spent" : [ ] }, { "_id" : ObjectId("5bd548390a84d90b5c2bb41c"), "dept" : "smoothing", "activity_time_spent" : [ ] }, { "_id" : ObjectId("5bd548390a84d90b5c2bb41e"), "dept" : "assembling", "activity_time_spent" : [ ] }, { "_id" : ObjectId("5bd548390a84d90b5c2bb420"), "dept" : "packaging", "activity_time_spent" : [ ] }, { "_id" : ObjectId("5bd54add0a84d90b5c2bb5c5"), "dept" : "shining", "activity_time_spent" : [ ] }, { "_id" : ObjectId("5bff847078c700209c1b515f"), "dept" : "shining", "activity_time_spent" : [ { "_id" : ObjectId("5c1d659083e7551854c1681d"), "time_spent" : 10, "shift" : "afternoon", }, { "_id" : ObjectId("5c1d659083e7551854c1681e"), "time_spent" : 10,  "shift" : "afternoon"} ] } ]}, "__v" : 14 }, 
{ "_id" : ObjectId("5bd548bf0a84d90b5c2bb45b"), "details" : {"first_name" : "Treasure", "other_names" : "Bliss", "last_name" : "Dwayne", "gender" : "female", "date_of_birth" : ISODate("2010-10-28T00:00:00Z") }, "contact" : [ { "_id" : ObjectId("5bd548bf0a84d90b5c2bb45c"), "residential_address" : "no 5 smith"} ], "health" : [ {"_id" : ObjectId("5bd548bf0a84d90b5c2bb45e"), "illness_name" : "Cold", "sypmtom" : "Sneezing", "normal_treatment" : ""} ], "activity_year" : { "_id" : ObjectId("5bd548bf0a84d90b5c2bb45f"), "activity_detail" : [ { "dept" : "spray", "activity_time_spent" : [ ] }, { "dept" : "smoothing", "activity_time_spent" : [ { "_id" : ObjectId("5c200ccea72a3e11f895dc26"), "time_spent" : 3, "shift" : "morning"}, { "_id" : ObjectId("5c215d2eea2ebe1c8043ccde"), "time_spent" : 5, "shift" : "morning"} ] }, { "dept" : "packaging", "activity_time_spent" : [ { "_id" : ObjectId("5c203e75084852185c583e13"), "time_spent" : 9,"shift" : "afternoon"}, { "_id" : ObjectId("5c20f50f4feffe0b00e8e9f7"), "time_spent" : 18,"shift" : "afternoon"} ] }, { "dept" : "shining", "activity_time_spent" : [ { "_id" : ObjectId("5c200b38a72a3e11f895dc08"), "time_spent" : 9, "shift" : "night", }, { "_id" : ObjectId("5c215acdea2ebe1c8043ccc0"), "time_spent" : 9,  "shift" : "night" } ] }] }, "__v" : 9 }, 

1 个答案:

答案 0 :(得分:0)

您的预期输出都不是有效的JSON文档,但我希望以下解决方案将引导您找到适合您需求的内容。

首先要动态创建文档密钥,您必须使用$arrayToObject运算符,该运算符期望将包含两个元素的数组作为输入。因此,您可以使用$addFields运算符将activity_year.activity_detail数组整形为这种格式。您可以使用$map来做到这一点,我假设您需要第一个time_spent(用于获取第一个元素的$arrayElemAt)。然后,您可以在下一阶段使用$concatArrays将该数据与name合并,请尝试:

db.col.aggregate([
    {
        $addFields: {
            activities: {
                $map: {
                    input: "$activity_year.activity_detail",
                    as: "activity",
                    in: {
                        $let: {
                            vars: {
                                first_time_spent: { $arrayElemAt: [ "$$activity.activity_time_spent", 0 ] }
                            },
                            in: [
                                [ "$$activity.dept", { $ifNull: [ "$$first_time_spent.time_spent", "" ] } ],
                                [ "shift", { $ifNull: [ "$$first_time_spent.shift", "" ] } ]
                            ]
                        }
                    }
                }         
            }
        }
    },
    {
        $project: {
            employee_details: {
                $concatArrays: [
                    [{ "name": { $concat: [ "$details.first_name", " ","$details.other_names", " ", "$details.last_name"] } }], 
                    {
                        $map: {
                            input: "$activities",
                            as: "a",
                            in: { $arrayToObject: "$$a" }
                        }
                    }
                ]
            }
        }
    }
])

以以下格式输出数据:

{
    "_id" : ObjectId("5bd548bf0a84d90b5c2bb45b"),
    "employee_details" : [
        {
            "name" : "Treasure Bliss Dwayne"
        },
        {
            "spray" : "",
            "shift" : ""
        },
        {
            "smoothing" : 3,
            "shift" : "morning"
        },
        {
            "packaging" : 9,
            "shift" : "afternoon"
        },
        {
            "shining" : 9,
            "shift" : "night"
        }
    ]
}