0
我有以下系列:MongoDB:如何在一个文档中分组嵌套数组?
{
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:[{},{},{}]
}
]
]
}
我找不出数组聚合或连接或联合或任何其他。 最终我将不得不做的产品也是一样的(获得所有项目的所有服务的所有产品作为产品的一个数组,并返回一个文件到服务器,但第一件事,第一......
10X
10倍,只是完美的阵列... – Erez