【问题标题】:mongodb node.js nested array fields concatenation single variable resultmongodb node.js 嵌套数组字段串联单变量结果
【发布时间】:2019-01-02 06:23:13
【问题描述】:

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

{$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 }, 

【问题讨论】:

  • 你能发布初始样本数据吗
  • 已发布示例数据。我会很感激任何

标签: arrays json mongodb variables


【解决方案1】:

您的预期输出都不是有效的 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"
        }
    ]
}

【讨论】:

  • 非常感谢。我会处理它并回复你。但我仍然非常感激。但是另一方面,请知道什么材料,例如书,网站,可以帮助我彻底学习和了解 mongodb/mongoose。我还没有真正能够得到一本真正有用和透彻的好书或材料。我想非常了解这件事,但我得到的书并不是真的有用。请问有什么推荐的。谢谢
  • @SMev 我不记得有什么特别的书,但我绝对可以推荐 mongodb 和 mongoose 文档——写得很好,有很多例子,所以这是学习 MongoDB IMO 的最佳方式
  • 感谢您的帮助。我很感激。我希望有一本好书可以真正有帮助。但我仍然处于一个小问题中。我希望我没有打扰你。我还有一个问题stackoverflow.com/questions/54053714/…。我是菜鸟
猜你喜欢
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 2012-03-25
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 2019-05-17
相关资源
最近更新 更多