将多个子文档数据合并为一个-猫鼬聚合

时间:2018-11-29 10:33:25

标签: node.js mongodb mongoose

我具有以下文档结构

[{
    "company": "company name 1",
    "location": "IN",
    "sectors" : ["sectors1","sectors2"],
    "casestudies" : [{
        "_id" : "234324easdasdasdd",
        "title" : "CN1 Casestudy 1",
        "pdf" : "asdasdasdasd.pdf"
    },{
        "_id" : "4564easdasdasdd",
        "title" : "CN1 Casestudy 2",
        "pdf" : "asdasdrf43asdasd.pdf"
    }],
    "pressreleases" : [{
        "_id" : "765324easdasdasdd",
        "title" : "CN1 pressreleases 1",
        "pdf" : "asd345sdasd.pdf"
    },{
        "_id" : "45764easdasdasdd",
        "title" : "CN1 pressreleases 2",
        "pdf" : "asda223asd.pdf"
    }]
    "companynews" : [{
        "_id" : "gfd65dasdd",
        "title" : "CN1 companynews 1",
        "pdf" : "fd6dasd.pdf"
    },{
        "_id" : "s56565d",
        "title" : "CN1 companynews 2",
        "pdf" : "j7sasasd.pdf"
    }]
},
{
    "company": "company name 2",
    "location": "US",
    "sectors" : ["sectors21","sectors22"],
    "casestudies" : [{
        "_id" : "ssasdasdasdd",
        "title" : "CN2 Casestudy 1",
        "pdf" : "asdasdasdasd.pdf"
    },{
        "_id" : "dasdasdd",
        "title" : "CN2 Casestudy 2",
        "pdf" : "asdasdrf43asdasd.pdf"
    }],
    "pressreleases" : [{
        "_id" : "asdasdd",
        "title" : "CN2 pressreleases 1",
        "pdf" : "asd345sdasd.pdf"
    },{
        "_id" : "asdasdd",
        "title" : "CN2 pressreleases 2",
        "pdf" : "asda223asd.pdf"
    }]
    "companynews" : [{
        "_id" : "jhgbfvasdd",
        "title" : "CN2 companynews 1",
        "pdf" : "fd6dasd.pdf"
    },{
        "_id" : "mnbvcsdfgy",
        "title" : "CN2 companynews 2",
        "pdf" : "j7sasasd.pdf"
    }]
}]

我试图实现此输出

{
    companys_details : [{
        "company": "company name 1",
        "location": "IN",
        "sectors" : ["sectors1","sectors2"]
    },{
        "company": "company name 2",
        "location": "US",
        "sectors" : ["sectors21","sectors22"]
    }],
    casestudies : [{
        "_id" : "234324easdasdasdd",
        "title" : "CN1 Casestudy 1",
        "pdf" : "asdasdasdasd.pdf"
    },{
        "_id" : "4564easdasdasdd",
        "title" : "CN1 Casestudy 2",
        "pdf" : "asdasdrf43asdasd.pdf"
    },{
        "_id" : "ssasdasdasdd",
        "title" : "CN2 Casestudy 1",
        "pdf" : "asdasdasdasd.pdf"
    },{
        "_id" : "dasdasdd",
        "title" : "CN2 Casestudy 2",
        "pdf" : "asdasdrf43asdasd.pdf"
    }],
    "pressreleases" : [{
        "_id" : "765324easdasdasdd",
        "title" : "CN1 pressreleases 1",
        "pdf" : "asd345sdasd.pdf"
    },{
        "_id" : "45764easdasdasdd",
        "title" : "CN1 pressreleases 2",
        "pdf" : "asda223asd.pdf"
    },{
        "_id" : "asdasdd",
        "title" : "CN2 pressreleases 1",
        "pdf" : "asd345sdasd.pdf"
    },{
        "_id" : "asdasdd",
        "title" : "CN2 pressreleases 2",
        "pdf" : "asda223asd.pdf"
    }],
    "companynews" : [{
        "_id" : "gfd65dasdd",
        "title" : "CN1 companynews 1",
        "pdf" : "fd6dasd.pdf"
    },{
        "_id" : "s56565d",
        "title" : "CN1 companynews 2",
        "pdf" : "j7sasasd.pdf"
    },{
        "_id" : "jhgbfvasdd",
        "title" : "CN2 companynews 1",
        "pdf" : "fd6dasd.pdf"
    },{
        "_id" : "mnbvcsdfgy",
        "title" : "CN2 companynews 2",
        "pdf" : "j7sasasd.pdf"
    }]
}

我尝试了一些解决方案:Solution1 Solution2Solution3和StackOverflow之外。

在第二次展开和分组之前,大多数解决方案都可以正常工作。但是无法达到确切的结果。

我尝试填充并汇总展开,分组和推送。

0 个答案:

没有答案