MongoDB嵌套文档展开问题

时间:2019-02-11 18:10:00

标签: mongodb multidimensional-array aggregation-framework embedded-documents

我有以下文档,我想获得以下输出...在这里可以帮忙吗??

   {
        "switchId" : 100,
        "switchName" : "Test switch",
        "switchDescription" : "Test switch",
        "switchCode" : "TestSwitch",
        "mnemonic" : "NIC",
        "profile" : [
                {
                        "profileName" : "xyz123",
                        "profileId" : 10
                },
                {
                        "profileName" : "QA1",
                        "profileId" : 1
                },
                {
                        "profileName" : "QA2",
                        "profileId" : 2
                }
        ],
        "switchServers" : [
                {
                        "servername" : "testServer123a",
                        "switchEnabled" : true
                },
                {
                        "servername" : "testServer456a",
                        "switchEnabled" : true
                },
                {
                        "servername" : "testServer789a",
                        "switchEnabled" : true
                },
                {
                        "servername" : "testServer123x",
                        "switchEnabled" : true
                },
                {
                        "servername" : "testServer123y",
                        "switchEnabled" : true
                },
                {
                        "servername" : "testServer123z",
                        "switchEnabled" : true
                },
                {
                        "servername" : "abc123",
                        "switchEnabled" : true
                }
        ]
}



I want to get the following output...

{    "switchId" : 100,
    "switchName" : "Test switch",
    "switchDescription" : "Test switch",
    "switchCode" : "TestSwitch",
    "mnemonic" : "NIC",
    "profile" : [ 
        {
            "profileName" : "xyz123",
            "profileId" : 10.0,
            "switchServers" : [ 
        {
            "servername" : "testServer123a",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer456a",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer789a",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer123x",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer123y",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer123z",
            "switchEnabled" : true
        }, 
        {
            "servername" : "abc123",
            "switchEnabled" : true
        }
    ]
        }, 
        {
            "profileName" : "QA1",
            "profileId" : 1.0,
            "switchServers" : [ 
        {
            "servername" : "testServer123a",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer456a",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer789a",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer123x",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer123y",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer123z",
            "switchEnabled" : true
        }, 
        {
            "servername" : "abc123",
            "switchEnabled" : true
        }
    ]
        }, 
        {
            "profileName" : "QA2",
            "profileId" : 2.0,
            "switchServers" : [ 
        {
            "servername" : "testServer123a",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer456a",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer789a",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer123x",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer123y",
            "switchEnabled" : true
        }, 
        {
            "servername" : "testServer123z",
            "switchEnabled" : true
        }, 
        {
            "servername" : "abc123",
            "switchEnabled" : true
        }
    ]
        }
    ]
    
})

0 个答案:

没有答案