【问题标题】:MongoDB: How to group nested arrays in one document?MongoDB:如何在一个文档中对嵌套数组进行分组?
【发布时间】:2020-05-06 22:31:18
【问题描述】:

我有以下收藏:

{
    id: 23423-dsfsdf-32423,
    name: Proj1,
    services: [
         {
            id:sdfs-24423-sdf,
            name:P1_Service1,
            products:[{},{},{}]
         },
         {
            id:sdfs-24jhh-sdf,
            name:P1_Service2,
            products:[{},{},{}]
         },
         {
            id:sdfs-2jnbn3-sdf,
            name:P1_Service3,
            products:[{},{},{}]
         }
    ]
},
{
    id: 23423-cxcvx-32423,
    name: Proj2,
    services: [
         {
            id:sdfs-xvxcv-sdf,
            name:P2_Service1,
            products:[{},{},{}]
         },
         {
            id:sdfs-xvwqw-sdf,
            name:P2_Service2,
            products:[{},{},{}]
         },
         {
            id:sdfs-erdfd-sdf,
            name:P2_Service3,
            products:[{},{},{}]
         }
    ]
}

我需要返回一个包含所有服务数组的文档:

{
    services: [
         {
            id:sdfs-24423-sdf,
            name:P1_Service1,
            products:[{},{},{}]
         },
         {
            id:sdfs-24jhh-sdf,
            name:P1_Service2,
            products:[{},{},{}]
         },
         {
            id:sdfs-2jnbn3-sdf,
            name:P1_Service3,
            products:[{},{},{}]
         },
         {
            id:sdfs-xvxcv-sdf,
            name:P2_Service1,
            products:[{},{},{}]
         },
         {
            id:sdfs-xvwqw-sdf,
            name:P2_Service2,
            products:[{},{},{}]
         },
         {
            id:sdfs-erdfd-sdf,
            name:P2_Service3,
            products:[{},{},{}]
         }
     ]
}

我得到的最多的是:

db.projects.aggregate({"$group":{"_id":"services","services":{"$push":"$services"}}})

但这会返回一个包含数组数组的文档,我想要一个对象数组:

{
    _id:"services",
    services:[
        [
            {
            id:sdfs-24423-sdf,
            name:P1_Service1,
            products:[{},{},{}]
         },
         {
            id:sdfs-24jhh-sdf,
            name:P1_Service2,
            products:[{},{},{}]
         },
         {
            id:sdfs-2jnbn3-sdf,
            name:P1_Service3,
            products:[{},{},{}]
         }
        ],
        [
            {
            id:sdfs-xvxcv-sdf,
            name:P2_Service1,
            products:[{},{},{}]
         },
         {
            id:sdfs-xvwqw-sdf,
            name:P2_Service2,
            products:[{},{},{}]
         },
         {
            id:sdfs-erdfd-sdf,
            name:P2_Service3,
            products:[{},{},{}]
         }
        ]
    ]
}

我无法弄清楚数组聚合或加入或联合或其他什么。 最终我也必须对产品做同样的事情(将所有项目的所有服务的所有产品作为一个产品数组并将文档返回到服务器,但首先要做的是......

10 倍

【问题讨论】:

    标签: node.js mongodb mongodb-query


    【解决方案1】:

    您需要在 null _id 上进行分组,以便所有 services 分组到单个文档中。 还有$unwind分组前的服务数组,否则组会给你数组数组

    db.project.aggregate(
      {$unwind: '$services'},
      {$group: {_id:null, services: {$push: '$services'}}}
    )
    

    【讨论】:

    • 在 OP 中,服务都有一个唯一的 ID。如果 id 不唯一,则结果包含重复的服务项。如何确保结果只包含独特的服务项目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    相关资源
    最近更新 更多