从聚合组收到的 MongoDB setIntersection 传递数组结果 [英] MongoDB setIntersection passing array result recieved from aggregrate group
问题描述
你好
我有一个 CompanyDepartmentJobRole 的集合,如下所示
CompanyID 部门 JobRoleID------------------------------------------1 D1 J11 D1 J21 D1 J31 D1 J41 D2 J51 D2 J1
我想使用 setIntersection 查找各个部门的 Common JobRole.
我可以分组并获取部门中的 JobRoles 数组,但现在我想将收到的结果传递给 setIntersection 但不知道如何提供它.
下面是对 deparmentes 进行分组并获取 JobRoles 数组的查询
db.CompanyDepartmentJobRole.aggregate( {$group : {_id : "$部门 ID",JobRole: { $addToSet: "$JobRoleID" }} } )
返回结果为
D1 D2---- ----J1 J5J2 J1J3J4
但不知道如何将这个结果传递给 setIntersection ,我正在尝试下面的查询但不是正确的方法
db.CompanyDepartmentJobRole.aggregate( {$group : {_id : "$部门 ID",JobRole: { $addToSet: "$JobRoleID" }} },{'$项目':{int:{$setIntersection:["$JobRole"]}}})
<块引用>
可以请任何人帮忙.
谢谢&问候阿杰
setIntersection
是一个投影运算符,不能用于查找两个不同文档中数组之间的交集.您可以使用以下聚合查询来计算出现在多个部门中的一组 JobRoleID
:
<代码>>db.so.aggregate([{$group: {_id: "$DepartmentID", JobRoleID: {$addToSet: "$JobRoleID"}}},{$unwind: "$JobRoleID"},{$group: {_id: "$JobRoleID", count: {$sum: 1}}},{$match: {count: {$gt: 1}}}])
首先计算一个部门中存在的 JobRoleID
的集合,然后计算每个 JobRoleID
在某个部门的集合中出现的次数,并且仅返回那些至少出现两次的那些.如果你想限制到特定部门,你可以在开始时放入一个 $match 阶段.
Hi
I have a collections of CompanyDepartmentJobRole as below
CompanyID Department JobRoleID
------------------------------------------
1 D1 J1
1 D1 J2
1 D1 J3
1 D1 J4
1 D2 J5
1 D2 J1
I want to use setIntersection to find the Common JobRole in Various Departments.
I am able to group by and get Array of JobRoles in the department, but now after this i want to pass the result received to the setIntersection but dont know how to provide it.
Below is the query to group deparmentes and get JobRoles Array
db.CompanyDepartmentJobRole.aggregate( {$group : {
_id : "$DepartmentID",
JobRole: { $addToSet: "$JobRoleID" }
} } )
which returns result as
D1 D2
---- ----
J1 J5
J2 J1
J3
J4
But dont know how to pass this result to setIntersection , i am trying below query but not correct way
db.CompanyDepartmentJobRole.aggregate( {$group : {
_id : "$DepartmentID",
JobRole: { $addToSet: "$JobRoleID" }
} },
{'$project': {
int:{$setIntersection:["$JobRole"]}
}
})
Could please anyone help.
Thanks & Regards Ajay
setIntersection
is a projection operator and can't be used to find the intersection between arrays in two different documents. You can compute the set of JobRoleID
s that are present in more than one department using the following aggregation query:
> db.so.aggregate([
{$group: {_id: "$DepartmentID", JobRoleID: {$addToSet: "$JobRoleID"}}},
{$unwind: "$JobRoleID"},
{$group: {_id: "$JobRoleID", count: {$sum: 1}}},
{$match: {count: {$gt: 1}}}
])
This starts off by computing the set of JobRoleID
s present in a department, then it counts the number of times each JobRoleID
occurs in the set of some department and only returns those which occur at least twice. If you want to restrict to specific departments, you can put in a $match stage at the beginning.
这篇关于从聚合组收到的 MongoDB setIntersection 传递数组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!