我有以下数据结构:
{
"groups" : [
{
"internalName" : "Group1",
"fields" : [
{
"internalName" : "Field1",
"uiProperties" : {
"isShow" : true
}
},
{
"internalName" : "Field2",
"uiProperties" : {
"isHide" : false
}
}
]
},
{
"internalName" : "Group2",
"fields" : [
{
"internalName" : "Field1",
"uiProperties" : {
"IsHide" : false
}
}
]
}
],
"internalName" : "Layout1"
},
{
"groups" : [
{
"internalName" : "Group3",
"fields" : [
{
"internalName" : "Field2",
"uiProperties" : {
"isShow" : true
}
},
{
"internalName" : "Field4",
"uiProperties" : {
"isHide" : false
}
}
]
},
{
"internalName" : "Group4",
"fields" : [
{
"internalName" : "Field3",
"uiProperties" : {
"IsHide" : false
}
}
]
}
],
"internalName" : "Layout2"
}
目标是按字段内部名称过滤此数据,并仅返回所需数据 - 仅包含仅包含所选内部名称的字段的组。例如,上面的数据: 过滤的内部名称 - " Field2"," Field4" 要返回的预期数据:
{
"internalName" : "Layout1",
"groups" : [
{
"internalName" : "Group1",
"fields" : [
{
"internalName" : "Field2",
"uiProperties" : {
"isHide" : false
}
}
]
}
]
},
{
"internalName" : "Layout2",
"groups" : [
{
"internalName" : "Group3",
"fields" : [
{
"internalName" : "Field4",
"uiProperties" : {
"isHide" : false
}
}
]
}
]
}
我尝试类似的东西
{
$match: {
"groups.fields.internalName": {
$in: ["Field2", "Field4"]
}
}
},
{
$unwind: "$groups"
},
{
$unwind: "$groups.fields"
},
{
$group: {
_id:"$_id",
internalName: {
$first: "$internalName",
},
groups:{
$push: {
internalName: "$groups.internalName",
fields: "$groups.fields"
}
}
}
}
但它运作不佳.. 你能帮帮我吗?
答案 0 :(得分:0)
汇总管道中存在错误。首先展开组元素,然后展开字段元素
{
{$unwind: "$groups"},
{$unwind: "$groups.fields"},
{$match: {"groups.fields.internalName", {$eq: {$or: ["Field2", "Field4",...]}}}},
}
稍后按照您的方式对其进行分组。