I have the following data structure:
{
"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"
}
The goal is filter this data by fields internal names and return only needed data - only groups which contain only fields with selected internal names. For example with data above: Filtered internal names - "Field2", "Field4" Expected data to return:
{
"internalName" : "Layout1",
"groups" : [
{
"internalName" : "Group1",
"fields" : [
{
"internalName" : "Field2",
"uiProperties" : {
"isHide" : false
}
}
]
}
]
},
{
"internalName" : "Layout2",
"groups" : [
{
"internalName" : "Group3",
"fields" : [
{
"internalName" : "Field4",
"uiProperties" : {
"isHide" : false
}
}
]
}
]
}
I'm try something like that
{
$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"
}
}
}
}
But it's not worked well.. Can you help me with it?
There is an error in your aggregation pipeline.. First unwind the group elements and then the fields element
Later group it the way you have done it.