我在使用$或查询的mongodb中遇到了小问题,我有一个包含以下模式的文档的集合。它有角色和 skill_set 键,每个角色都有一些相关的技能组合。
[
{
"skill_set" : [
{
"skill_name" : "Project Planning"
},
{
"skill_name" : "Delivery Management"
},
{
"skill_name" : "Resource Management"
},
{
"skill_name" : "Stakeholder Management"
},
{
"skill_name" : "Agile Project Management"
},
{
"skill_name" : "Change Management"
}
],
"role" : "Project Manager"
},
{
"skill_set" : [
{
"skill_name" : "Project Planning"
},
{
"skill_name" : "Delivery Management"
},
{
"skill_name" : "Resource Management"
},
{
"skill_name" : "Stakeholder Management"
},
{
"skill_name" : "Agile Project Management"
},
{
"skill_name" : "Change Management"
},
{
"skill_name" : "Financial Management"
},
{
"skill_name" : "Sales, Marketing and Customer Service "
},
{
"skill_name" : "Strategic Input"
}
],
"role" : "Sr Project Manager"
},
{
"skill_set" : [
{
"skill_name" : "Project Planning"
},
{
"skill_name" : "Delivery Management"
},
{
"skill_name" : "Resource Management"
},
{
"skill_name" : "Stakeholder Management"
},
{
"skill_name" : "Agile Project Management"
},
{
"skill_name" : "Change Management"
},
{
"skill_name" : "Financial Management"
},
{
"skill_name" : "Sales, Marketing and Customer Service "
},
{
"skill_name" : "Strategic Input"
}
],
"role" : "Delivery Manager"
},
{
"skill_set" : [
{
"skill_name" : "Test Planning"
},
{
"skill_name" : "Technlogy Expertise"
},
{
"skill_name" : "Team Player"
},
{
"skill_name" : "Communication"
},
{
"skill_name" : "Design and Architecture"
},
{
"skill_name" : "Requirement Understanding"
},
{
"skill_name" : "Task Management"
}
],
"role" : "Tester"
}
]
我想搜索匹配skill_set的角色的文档,我可以使用查询来获取。
{"$or":[{"skill_set.skill_name":"Project Planning"},{"skill_set.skill_name":"Strategic Input"},{"skill_set.skill_name":"Delivery Management"}]},{"_id":0}
查询输出
[
{
"skill_set" : [
{
"skill_name" : "Project Planning"
},
{
"skill_name" : "Delivery Management"
},
{
"skill_name" : "Resource Management"
},
{
"skill_name" : "Stakeholder Management"
},
{
"skill_name" : "Agile Project Management"
},
{
"skill_name" : "Change Management"
}
],
"role" : "Project Manager"
},
{
"skill_set" : [
{
"skill_name" : "Project Planning"
},
{
"skill_name" : "Delivery Management"
},
{
"skill_name" : "Resource Management"
},
{
"skill_name" : "Stakeholder Management"
},
{
"skill_name" : "Agile Project Management"
},
{
"skill_name" : "Change Management"
},
{
"skill_name" : "Financial Management"
},
{
"skill_name" : "Sales, Marketing and Customer Service "
},
{
"skill_name" : "Strategic Input"
}
],
"role" : "Sr Project Manager"
},
{
"skill_set" : [
{
"skill_name" : "Project Planning"
},
{
"skill_name" : "Delivery Management"
},
{
"skill_name" : "Resource Management"
},
{
"skill_name" : "Stakeholder Management"
},
{
"skill_name" : "Agile Project Management"
},
{
"skill_name" : "Change Management"
},
{
"skill_name" : "Financial Management"
},
{
"skill_name" : "Sales, Marketing and Customer Service "
},
{
"skill_name" : "Strategic Input"
}
],
"role" : "Delivery Manager"
}
]
在输出中,它清晰可见,提取了3个角色。除此之外,我想要匹配角色.ie的技能组数。
在给定的查询中,我正在使用项目计划或交付管理或战略输入搜索角色,我希望技能管理员在技能组合中有2匹配技能管理员,我希望技能为物品管理员2匹配,即我想要下面的内容。
[
{
"role" : "Project Manager",
"skill_match_count" : 2
},
{
"role" : "Sr Project Manager",
"skill_match_count" : 3
},
{
"role" : "Delivery Manager",
"skill_match_count" : 3
}
]
答案 0 :(得分:1)
您需要做的是$filter
skill_set
数组与$or
条件,然后$project
过滤数组的$size
:< / p>
db.roles.aggregate([
{
$project: {
role: 1,
filtered_skill_set: {
$filter: {
input: "$skill_set",
as: "skill",
cond: {
$or: [
{ $eq: ["$$skill.skill_name", "Project Planning"] },
{ $eq: ["$$skill.skill_name", "Strategic Input"] },
{ $eq: ["$$skill.skill_name", "Delivery Management"] }
]
}
}
}
}
},
{
$project: {
role: 1,
skill_match_count: {
$size: "$filtered_skill_set"
}
}
}
])
这将产生您期望的输出,包括skill_match_count
中包含0的角色。如果要排除它们,可以在最后添加另一个$match
管道阶段:
{
$match: {
skill_match_count: {
$gt: 0
}
}
}