具有独立集合的Mongodb聚合
我对超过2个集合的mongodb聚合有问题。 这里我代表我的收藏。具有独立集合的Mongodb聚合,mongodb,mongoose,Mongodb,Mongoose,我对超过2个集合的mongodb聚合有问题。 这里我代表我的收藏。 第一集:潜力 { "_id" : ObjectId("5a0d1cb1d2fffa95ed85b3ba"), "potential_id" : "P-00012347", "potential_name" : "NKCL"} 我的第二个收藏:办公室 { "_id" : ObjectId("5a0d20e8d2fffa95ed85b5bc"), "potential_id" : "P-00012347", "potenti
第一集:潜力
{
"_id" : ObjectId("5a0d1cb1d2fffa95ed85b3ba"),
"potential_id" : "P-00012347",
"potential_name" : "NKCL"}
我的第二个收藏:办公室
{
"_id" : ObjectId("5a0d20e8d2fffa95ed85b5bc"),
"potential_id" : "P-00012347",
"potential_name" : "NKCL",
"revision_id" : "R0",
"office_name" : "Marketing office",
"vertical_info" : [
{
"vertical_name" : "MEP",
"estimated_team" : "Marketing"
},
{
"vertical_name" : "BIM",
"estimated_team" : "Marketing"
},
{
"vertical_name" : "V1",
"estimated_team" : "Marketing"
}
]}
我的第三个收藏:服务
{
"_id" : ObjectId("5a0d212cd2fffa95ed85b5e6"),
"potential_id" : "P-00012347",
"potential_name" : "NKCL",
"revision_id" : "R0",
"office_name" : "Marketing office",
"vertical_name" : "MEP",
"service_info" : [
{
"service_name" : "Service 1",
"total_cost" : 1
},
{
"service_name" : "Service 2",
"total_cost" : 2
}
]}
我的第四个收藏:servicebuild
{
"_id" : ObjectId("5a0d2175d2fffa95ed85b612"),
"potential_id" : "P-00012347",
"potential_name" : "NKCL",
"revision_id" : "R0",
"office_name" : "Marketing office",
"vertical_name" : "MEP",
"service_name" : "Service 1",
"service_building_info" : [
{
"building_no" : 1,
"building_name" : "Building 1"
},
{
"building_no" : 2,
"building_name" : "Building 2"
},
{
"building_no" : 3,
"building_name" : "Building 3"
}
]}
现在,我想在一个聚合查询中加入上述4个集合。我看到的大多数示例仅连接两个集合
我的输出是这样的
{
"_id" : ObjectId("5a0d1cb1d2fffa95ed85b3ba"),
"potential_id" : "P-00012347",
"potential_name" : "NKCL"
"revision_id" : "R0",
"office_name" : "Marketing office",
"vertical_info" : [
{
"vertical_name" : "MEP",
"estimated_team" : "Marketing"
"service_info" : [
{
"service_name" : "Service 1",
"total_cost" : 1
"service_building_info" : [
{
"building_no" : 1,
"building_name" : "Building 1"
},
{
"building_no" : 2,
"building_name" : "Building 2"
},
{
"building_no" : 3,
"building_name" : "Building 3"
}
]
},
{
"service_name" : "Service 2",
"total_cost" : 2
}
]
},
{
"vertical_name" : "BIM",
"estimated_team" : "Marketing"
},
{
"vertical_name" : "V1",
"estimated_team" : "Marketing"
}]}
任何人都可以帮助解决我的问题。以下是产生所需输出的聚合:
db.potential.aggregate([{
$lookup:{
从‘办公室’,
localField:“潜在id”,
foreignField:“潜在id”,
as:‘办公室’
}
},
{
$unwind:“$office”
},
{
$项目:{
潜在用户id:1,
潜在客户名称:1,
修订号:“$office.revision\u id”,
办公室名称:“$office.office\u name”,
垂直信息:“$office.vertical\u info”
}
},
{
$lookup:{
来自:'服务',
localField:'垂直\u信息.垂直\u名称',
foreignField:'垂直名称',
as:‘服务’
}
},
{
$项目:{
潜在用户id:1,
潜在客户名称:1,
修订编号:1,
办公室名称:1,
垂直信息:{
$map:{
输入:“$vertical\u info”,
as:“垂直信息项目”,
在:{
垂直\u名称:“$$vertical\u info\u item.vertical\u name”,
估计的团队:“$$vertical\u info\u item.估计的团队”,
服务信息:{
$filter:{
输入:“$services”,
作为‘服务’,
条件:{
$eq:['$$vertical\u info\u item.vertical\u name','$$service.vertical\u name']
}
}
}
}
}
}
}
},
{
$项目:{
潜在用户id:1,
潜在客户名称:1,
修订编号:1,
办公室名称:1,
垂直信息:{
$map:{
输入:“$vertical\u info”,
as:“垂直信息项目”,
在:{
垂直\u名称:“$$vertical\u info\u item.vertical\u name”,
估计的团队:“$$vertical\u info\u item.估计的团队”,
服务信息:{
$arrayElemAt:['$$vertical\u info\u item.service\u info.service\u info',0]
}
}
}
}
}
},
{
$lookup:{
来自:“servicebuild”,
localField:'垂直信息.服务信息.服务名称',
foreignField:'服务名称',
as:'servicebuild'
}
},
{
$项目:{
潜在用户id:1,
潜在客户名称:1,
修订编号:1,
办公室名称:1,
垂直信息:{
$map:{
输入:“$vertical\u info”,
as:“垂直信息项目”,
在:{
垂直\u名称:“$$vertical\u info\u item.vertical\u name”,
估计的团队:“$$vertical\u info\u item.估计的团队”,
服务信息:{
$map:{
输入:“$$vertical\u info\u item.service\u info”,
as:“服务信息项目”,
在:{
服务名称:“$$service\u info\u item.service\u name”,
总成本:“$$service\u info\u项目。总成本”,
服务楼信息:{
$filter:{
输入:“$servicebuild”,
as:'servicebuild_item',
条件:{
$eq:['$$service\u info\u item.service\u name','$$servicebuild\u item.service\u name']
}
}
}
}
}
}
}
}
}
}
},
{
$项目:{
潜在用户id:1,
潜在客户名称:1,
修订编号:1,
办公室名称:1,
垂直信息:{
$map:{
输入:“$vertical\u info”,
as:“垂直信息项目”,
在:{
$cond:{
如果:{
$ne:['$$vertical\u info\u item.service\u info',null]
},
然后:{
垂直\u名称:“$$vertical\u info\u item.vertical\u name”,
估计的团队:“$$vertical\u info\u item.估计的团队”,
服务信息:{
$map:{
输入:“$$vertical\u info\u item.service\u info”,
as:“服务信息项目”,
在:{
服务名称:“$$service\u info\u item.service\u name”,
总成本:“$$service\u info\u项目。总成本”,
服务楼信息:{
$filter:{
输入:“$$service\u info\u item.service\u building\u info”,
as:'servicebuild_item',
条件:{
$eq:['$$service\u info\u item.service\u name','$$servicebuild\u item.service\u name']
}
}
}
}
}
}
},
其他:{
垂直\u名称:“$$vertical\u info\u item.vertical\u name”,
估计的团队:“$$vertical\u info\u项目。估计的团队”
}
}
}
}
}
}
},
{
$项目:{
潜在用户id:1,
潜在客户名称:1,
修订编号:1,
办公室名称:1,
垂直信息:{
$map:{
输入:“$vertical\u info”,
as:“垂直信息项目”,
在:{
$cond:{
如果:{
$ne:['$$vertical\u info\u item.service\u info',未定义]
},
然后:{
垂直\u名称:“$$vertical\u info\u item.vertical\u name”,
估计的团队:“$$vertical\u info\u item.估计的团队”,
服务信息:{
$map:{
输入:“$$vertical\u info\u item.service\u info”,
as:“服务信息项目”,