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