【发布时间】:2017-11-03 07:27:40
【问题描述】:
我正在使用 mongodb 3.4。运行我的聚合后,我的结果如下所示。我做了两个集合之间的连接。
[ {
"_id": { // rename _id to main
"id": "ID_001",
"name": "Fred flinstone Inc"
},
"types": [
{
"typeId": "TYPE1",
"locations": [
{
"locationName": "Sydney", // rename locationName to name
"units": [
{
"unitId": "PHG_BTG1" // remove the unitId, i just want the value
}
]
},
{
"locationName": "Brisbane",
"units": [
{
"unitId": "PHG_KTN1"
},
{
"unitId": "PHG_KTN2"
}
]
}
]
}
] } ]
我想把它投影成
[
{
"main": {
"id": "ID_001",
"name": "Fred flinstone Inc"
},
"types": [
{
"typeId": "TYPE1",
"locations": [
{
"name": "Sydney",
"units": [
"PHG_BTG1"
]
},
{
"name": "Brisbane",
"units": [
"PHG_KTN1",
"PHG_KTN2"
]
}
]
}
]
}
]
我该怎么做?我尝试了 $project 的各种组合但失败了。 示例
{ $project: {
main: "$_id",
"_id": 0,
"types.locations.name": "$types.locations.locationName"
}}
将正确重命名locations.name,但该值显示一个数组 [Sydney, Brisbane] 。当我尝试类似于 locations.units 时也是如此。
我试图再次放松,但它会显示一个空的结果。非常感谢任何帮助。
【问题讨论】:
标签: mongodb aggregation-framework