如何将现有的字符串数组转换为mongodb中的自定义对象数组?

时间:2018-08-06 07:37:34

标签: mongodb aggregation-framework

我有一些现有数据,我想对其进行更新,但是我无法为此建立查询。 我想将字符串数组转换为对象数组并更新选项字段。 有可能吗?

我尝试了arrayToObject但它不起作用 以下是我现有的记录:

{
    "_id" : ObjectId("5b6455d9c006ae9d142b0da8"),
    "PartnerId" : "585938e3d4e9dac9bb2b09c6",
    "BusinessID" : NumberLong(98),
    "Responses" : [ 
        {
            "QID" : 1,
            "Order" : 1,
            "Question" : "Contact Information 1",
            "Options" : [ 
                "First Name", 
                "Address", 
                "Email", 
                "Phone"
            ],
            "Answers" : [ 
                "First Name", 
                "111, Dublin, California, 94568", 
                "forms1@vagaro.com", 
                "111"
            ]
        },        
        {
            "QID" : 8,
            "Order" : 6,
            "Question" : "Contact Information 2",            
            "Options" : [ 
                "Address",
                "Email" 
            ],
            "Answers" : [ 
                "5000 Estate Enighed, Independence, Kansas, 67301"
            ]
        }
    ]
}

预期结果:

{
    "_id" : ObjectId("5b6455d9c006ae9d142b0da8"),
    "PartnerId" : "585938e3d4e9dac9bb2b09c6",
    "BusinessID" : NumberLong(98),
    "Responses" : [ 
        {
            "QID" : 1,
            "Order" : 1,
            "Question" : "Contact Information 1",
            "Options" : [ 
                {Option:"First Name", Order:1}, 
                {Option:"Address", Order:2}, 
                {Option:"Email", Order:3}, 
                {Option:"Phone", Order:4}
            ],
            "Answers" : [ 
                "First Name", 
                "111, Dublin, California, 94568", 
                "forms1@vagaro.com", 
                "111"
            ]
        },        
        {
            "QID" : 8,
            "Order" : 6,
            "Question" : "Contact Information 2",            
            "Options" : [ 
                {Option:"Address", Order:1},
                {Option:"Email" , Order:2}
            ],
            "Answers" : [ 
                "5000 Estate Enighed, Independence, Kansas, 67301"
            ]
        }
    ]
}

请帮助我。 谢谢

1 个答案:

答案 0 :(得分:0)

您可以如下使用Aggregation。我使用了与您描述的文档相同的mongo shell。

db.test.aggregate([
    { $match: {} },
    {
        $project: {
            _id: "$_id",
            PartnerId: "$PartnerId",
            BusinessID: "$BusinessID",
            Responses: {
                $map: {
                    input: "$Responses",
                    as: 'response',
                    in: {
                         "QID" : "$$response.QID",
                         "Order" : "$$response.Order",
                         "Question" : "$$response.Question",
                         "Options" : {
                             $map: {
                                 input: "$$response.Options",
                                 as: 'option',
                                 in: {
                                     Option: "$$option",
                                     Order: {
                                         $sum: [
                                             {
                                                 $indexOfArray: [
                                                     "$$response.Options",
                                                     "$$option"
                                                 ]
                                             },
                                             1
                                         ]
                                     }
                                 }
                             }
                         },
                         "Answers" : "$$response.Answers"
                    }
                }
            }
        }
    },
    { $out: 'output' } 
])

将所需的文档输出到集合output。您可以检查它并稍后重命名,或者如果要覆盖/创建另一个集合,只需在$out阶段指定另一个集合名称。